Remove duplicates in sorted array
Given an array in ascending order nums
, please delete repeated elements in place so that each element appears only once, and return the new length of the deleted array. The relative order of elements should be consistent. Then returns the number of unique elements nums
in .
Considering nums
that the number of unique elements is k
, you need to do the following to ensure that your solution will pass:
- Changes the array
nums
sonums
thatk
the first elements of contain unique elements, in the order they originally appearednums
in .nums
The size of the remaining elementsnums
of is not important. - return
k
.
Judgment criteria:
The system will test your solution with the following code:
int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 长度正确的期望答案
int k = removeDuplicates(nums); // 调用
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will pass.
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: The function should return a new length of 2, and the first two elements of the original array nums are modified to 1, 2. Elements beyond the new length in the array do not need to be considered.
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]
Explanation: The function should return the new length 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4. Elements beyond the new length in the array do not need to be considered.
hint:
1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums are sorted in ascending order
This is one 双指针问题
. To solve this problem, we can use two pointers, a slow pointer j i
and a fast pointer j. The slow pointer i
is used to point to the last position of the current non-repeated element, and the fast pointer j
is used to traverse the array. During the traversal process, if the element pointed to j
by i
is different from the element pointed to by the slow pointer, it means that a new non-repeating element is encountered, and we will put the new non-repeating element in the next position i
of Slow hand Moves one position i
backward . When the traversal ends, the position i
of plus 1
is the length of the new array.
Follow the steps below:
-
Initializes two pointers
i
andj
, both pointing to the first element of the array. -
Traverse the array until the fast pointer
j
reaches the end of the array.
a. Ifnums[i] != nums[j]
, means that a new non-repeating element is encountered,nums[j]
copy to itnums[i+1]
, and then move the slow pointeri
backward by one position.b. Move the fast pointer
j
back one position. -
After the traversal, return
i+1
, which is the length of the new array.
int removeDuplicates(vector<int>& nums) {
if (nums.size() == 0) return 0; // 如果数组为空,直接返回 0
int i = 0; // 定义慢指针 i,初始时指向数组的第一个元素
for (int j = 1; j < nums.size(); j++) {
// 定义快指针 j,从数组的第二个元素开始遍历
if (nums[i] != nums[j]) {
// 如果快指针 j 指向的元素与慢指针 i 指向的元素不同
i++; // 将慢指针 i 向后移动一个位置
nums[i] = nums[j]; // 将快指针 j 指向的元素复制到慢指针 i 的下一个位置
}
}
return i + 1; // 返回新数组的长度
}
int removeDuplicates(vector<int>& nums) {
if (nums.size() == 0) return 0; // 如果数组为空,直接返回 0
int i = 0, j = 1; // 定义慢指针 i 和快指针 j,初始时分别指向数组的第一个元素和第二个元素
while (j < nums.size()) {
// 只要快指针 j 没有到达数组末尾
if (nums[i] != nums[j]) {
// 如果快指针 j 指向的元素与慢指针 i 指向的元素不同
i++; // 将慢指针 i 向后移动一个位置
nums[i] = nums[j]; // 将快指针 j 指向的元素复制到慢指针 i 的下一个位置
}
j++; // 将快指针 j 向后移动一个位置
}
return i + 1; // 返回新数组的长度
}
Detailed process steps:
-
Check the array length and return 0 if it is 0.
-
Initialize two pointers: slow pointer i and fast pointer j. i points to the first element of the array and j points to the second element of the array.
-
Traverse the array until the fast pointer j reaches the end of the array.
a. Determine whether nums[i] is not equal to nums[j].
b. If nums[i] is not equal to nums[j], it means that a new unique element is encountered.
i. Move the slow pointer i backward one position.
ii. Copy nums[j] to nums[i].
c. If nums[i] is equal to nums[j], do nothing.
d. Move the fast pointer j backward one position.
-
When the traversal is over, return i + 1, which is the length of the new array.
As an example, consider the array nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4].
initial state: i = 0, j = 1 nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
Traversal process:
- First loop:
nums[i] == nums[j]
, do nothing, j = 2. - Second loop:
nums[i] != nums[j],i = 1,nums[i] = nums[j],j = 3。 nums = [0, 1, 1, 1, 1, 2, 2, 3, 3, 4]
- The third cycle:
nums[i] == nums[j]
, no operation is performed, j = 4. - The fourth cycle:
nums[i] == nums[j]
, no operation is performed, j = 5. - Fifth cycle:
nums[i] != nums[j],i = 2,nums[i] = nums[j],j = 6。 nums = [0, 1, 2, 1, 1, 2, 2, 3, 3, 4]
- The sixth cycle:
nums[i] == nums[j]
, no operation is performed, j = 7. - Seventh cycle:
nums[i] != nums[j],i = 3,nums[i] = nums[j],j = 8。 nums = [0, 1, 2, 3, 1, 2, 2, 3, 3, 4]
- The eighth cycle:
nums[i] == nums[j]
, no operation is performed, j = 9. - Ninth loop:
nums[i] != nums[j],i = 4,nums[i] = nums[j],j = 10
(Break out of the loop because j reaches the end of the array).nums = [0, 1, 2, 3, 4, 2, 2, 3, 3, 4]
The loop ends.
At this point i = 4
, the length of the new array is i + 1 = 5
. The new array is[0, 1, 2, 3, 4]。