-
Notifications
You must be signed in to change notification settings - Fork 44
/
_26_removeDuplicates.java
63 lines (59 loc) · 2.26 KB
/
_26_removeDuplicates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package pp.arithmetic.leetcode;
/**
* Created by wangpeng on 2019-03-22.
* 26. 删除排序数组中的重复项
* <p>
* 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
* <p>
* 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
* <p>
* 示例 1:
* <p>
* 给定数组 nums = [1,1,2],
* <p>
* 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
* <p>
* 你不需要考虑数组中超出新长度后面的元素。
* 示例 2:
* <p>
* 给定 nums = [0,0,1,1,1,2,2,3,3,4],
* <p>
* 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
* <p>
* 你不需要考虑数组中超出新长度后面的元素。
* 说明:
* <p>
* 为什么返回数值是整数,但输出的答案是数组呢?
* <p>
* 请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
* <p>
* 你可以想象内部操作如下:
* <p>
* // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
* int len = removeDuplicates(nums);
* <p>
* // 在函数里修改输入数组对于调用者是可见的。
* // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
* for (int i = 0; i < len; i++) {
* print(nums[i]);
* }
*
* @see <a href="https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/">remove-duplicates-from-sorted-array</a>
*/
public class _26_removeDuplicates {
public static void main(String[] args) {
_26_removeDuplicates removeDuplicates = new _26_removeDuplicates();
System.out.println(removeDuplicates.removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}));
}
public int removeDuplicates(int[] nums) {
int preNumIndex = 0, lastIndex = 0;
for (int index = 1; index < nums.length; index++) {
if (nums[index] > nums[preNumIndex]) {
lastIndex++;
nums[lastIndex] = nums[index];
preNumIndex = index;
}
}
return lastIndex + 1;
}
}