-
Notifications
You must be signed in to change notification settings - Fork 0
/
26. Remove Duplicates from Sorted Array
107 lines (82 loc) · 4.43 KB
/
26. Remove Duplicates from Sorted Array
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element
appears only once. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in
the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k
elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementation
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
1 <= nums.length <= 3 * 104
-100 <= nums[i] <= 100
nums is sorted in non-decreasing order.
给定一个整数数组nums,它是以非递减顺序排序的。请就地删除数组中的重复元素,使得每个唯一元素仅出现一次。元素的相对顺序应保持不变。
由于在某些语言中无法改变数组的长度,因此您必须将结果放置在数组nums的前面。更形式化地说,如果在删除重复项后有k个元素,则nums的前k个元素应保存最终结果。
您在前k个元素之后留下什么无关紧要。
在不分配额外空间的情况下,通过修改输入数组来实现O(1)的额外内存。
自定义评判:
评判将用以下代码测试您的解决方案:
int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 预期答案的正确长度
int k = removeDuplicates(nums); // 调用你的实现
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
如果所有断言都通过,那么你的解决方案将被接受。
示例1:
输入:nums = [1,1,2]
输出:2,nums = [1,2,_]
解释:你的函数应该返回k = 2,nums的前两个元素分别是1和2。
在返回的k之后,你留下什么无关紧要(因此它们是下划线)。
示例2:
输入:nums = [0,0,1,1,1,2,2,3,3,4]
输出:5,nums = [0,1,2,3,4,,,,,_]
解释:你的函数应该返回k = 5,nums的前五个元素分别是0,1,2,3和4。
在返回的k之后,你留下什么无关紧要(因此它们是下划线)。
约束:
1 <= nums.length <= 3 * 10^4
-100 <= nums[i] <= 100
nums是按非递减顺序排序的。
class Solution {
public int removeDuplicates(int[] nums) {
// 如果数组长度为0,返回0
if (nums.length == 0)
return 0;
// 定义一个变量addIndex,表示唯一元素要插入的索引位置
int addIndex = 1;
// 遍历数组,从第一个元素开始到倒数第二个元素
for (int i = 0; i < nums.length - 1; i++) {
// 如果当前元素小于下一个元素,说明下一个元素是一个新的唯一元素
if (nums[i] < nums[i + 1]) {
// 将新的唯一元素插入到addIndex位置
nums[addIndex] = nums[i + 1];
// 更新addIndex,指向下一个要插入的位置
addIndex++;
}
}
// 返回唯一元素的个数
return addIndex;
}
}
这段代码使用一个指针(addIndex)记录不重复元素应该插入的位置。遍历数组中的元素,如果当前元素小于下一个元素,
则表示下一个元素是一个新的唯一元素,将其插入到addIndex位置,并更新addIndex。最后返回addIndex作为去重后数组的长度。