您的位置:首页 > 其它

LeetCode 分类练习(1)—— 在数组中移动指定元素、删除指定元素、删除重复元素

2017-11-22 23:32 495 查看

283. Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.


将数组中的 0 移动到数组末尾,保持非零元素的原有顺序。

必须原地进行。

思路:

遍历 nums 数组,遇到非 0 的元素,将其从第一个元素开始依次赋值。

将剩下的未赋值的元素置为 0

package com.leetcode.array;

// 283 MoveZeroes

public class MoveZeroes {
// version-1 时间复杂度 O(n) , 空间复杂度 O(n)
//    public static int[] moveZeroes(int[] nums) {
//        int n = nums.length;
//        int[] aux = new int
;
//        int k = 0;
//        for (int i = 0; i < n; i++)
//            if (nums[i] != 0)
//                aux[k++] = nums[i];
//        while (k < n)
//            aux[k++] = 0;
//        return aux;
//    }

// version-2 时间复杂度 O(n),空间复杂度 O(1)
//    public static void moveZeroes(int[] nums) {
//        int n = nums.length;
//        int k = 0;  // nums[0...k) != 0     nums[k...i) == 0
//        for (int i = 0; i < n; i++)
//            if (nums[i] != 0)
//                if (i != k) // 防止整个数组全部是非0元素
//                    swap(nums, i, k++);
//                else
//                    k++;
//
//    }

public static void moveZeroes(int[] nums) {
//        int n = nums.length;
//        int k = 0;
//        for (int i = 0; i < n; i++) {
//            if (nums[i] != 0)
//                nums[k++] = nums[i];
//        }
//        while (k < n)
//            nums[k++] = 0;

int k = 0;
for (int num:nums)
if (num != 0)
nums[k++] = num;
while (k<nums.length)
nums[k++] = 0;
}

public static void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr;
arr[b] = temp;
}

public static void main(String[] args) {
int[] nums = {0, 1, 0, 3, 12};
//        int[] nums = {2, 1};

//        int[] aux = MoveZeroes.moveZeroes(nums);
MoveZeroes.moveZeroes(nums);
for (int num : nums)
System.out.println(num);
}

}


[b]27. Remove Element

Given an array and a value, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.


移除数组中全部指定值,返回数组的新长度。

不许使用额外空间。

数组中元素的顺序可以改变。

思路:

1、遍历 nums 数组,将不等于指定 val 的元素从 nums 数组头部开始依次赋值

2、返回 最后一个被赋值的下标 +1 ,即为新数组的长度

package com.leetcode.array;

// 27 RemoveElement

public class RemoveElement {
public int removeElement(int[] nums, int val) {
//        int n = nums.length;
//        int k = 0;
//        for (int i = 0; i < n; i++) {
//            if (nums[i] != val)
//                nums[k++] = nums[i];
//        }
//        return k;

int k = 0;
for (int num : nums)
if (num != val)
nums[k++] = num;
return k;
}

}


26. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.


给出一个已排序的数组,移除重复的元素,保证每个元素只出现一次,返回新的长度。

不允许使用额外空间。

思路:

1、在 [0…k) 放置非重复的元素

2、遍历 nums 数组,如果当前遍历到的元素 num > nums[k-1] ,则 num 为非重复元素,将其放到 nums[k] 处

3、k++

4、返回 k,即为非重复的数组长度

package com.leetcode.array;

// 26 RemoveDuplicates

public class RemoveDuplicates {
public int removeDuplicates(int[] nums) {
//        int n = nums.length;
//        int k = 0;  // nums[0...k] 为非重复的元素      nums(k...i) 为重复元素
//        for (int i = 0; i < n; i++) {
//            if (nums[i] != nums[k]) {
//                nums[++k] = nums[i];
//            }
//        }
//        return k + 1;

int k = 0;  // nums[0...k) 元素非重复
for (int num : nums)
if (k < 1 || num > nums[k - 1])
nums[k++] = num;
return k;
}

public static void main(String[] args) {
int[] nums = {1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 6};
RemoveDuplicates removeDuplicates = new RemoveDuplicates();
System.out.println(removeDuplicates.removeDuplicates(nums));
for (int num : nums)
System.out.print(num + " ");
}
}


80. Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.


和上一道题差不多,不过每个元素至多出现两次

思路也和上一道题差不多…

package com.leetcode.array;

public class RemoveDuplicatesII {

public int removeDuplicatesII(int[] nums) {
//        int n = nums.length;
//        int k = 0;
//        int count = 0;
//        for (int i = 0; i < n; i++) {
//            if (nums[i] != nums[k]) {
//                nums[++k] = nums[i];
//                count = 1;
//            } else if (nums[i] == nums[k] && count == 0) {
//                count++;
//            } else if (nums[i] == nums[k] && count < 2) {
//                nums[++k] = nums[i];
//                count++;
//            }
//        }
//        return k + 1;

int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;

}

public static void main(String[] args) {
//        int[] nums = {1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 6};
int[] nums = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4};
//        int[] nums = {};
RemoveDuplicatesII removeDuplicates = new RemoveDuplicatesII();
System.out.println(removeDuplicates.removeDuplicatesII(nums));
for (int num : nums)
System.out.print(num + "");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐