26. Remove Duplicates from Sorted Array
Description :
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.
Hint 1
In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image above for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements?
Hint 2
We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements.
Hint3
Essentially, once an element is encountered, you simply need to bypass its duplicates and move on to the next unique element.
Solution1 快慢指针
这道题目的要求是:对给定的有序数组 \textit{nums}nums 删除重复元素,在删除重复元素之后,每个元素只出现一次,并返回新的长度,上述操作必须通过原地修改数组的方法,使用 O(1)O(1) 的空间复杂度完成。
由于给定的数组 \textit{nums}nums 是有序的,因此对于任意 i<ji<j,如果 \textit{nums}[i]=\textit{nums}[j]nums[i]=nums[j],则对任意 i \le k \le ji≤k≤j,必有 \textit{nums}[i]=\textit{nums}[k]=\textit{nums}[j]nums[i]=nums[k]=nums[j],即相等的元素在数组中的下标一定是连续的。利用数组有序的特点,可以通过双指针的方法删除重复元素。
如果数组 \textit{nums}nums 的长度为 00,则数组不包含任何元素,因此返回 00。
当数组 \textit{nums}nums 的长度大于 00 时,数组中至少包含一个元素,在删除重复元素之后也至少剩下一个元素,因此 \textit{nums}[0]nums[0] 保持原状即可,从下标 11 开始删除重复元素。
定义两个指针 \textit{fast}fast 和 \textit{slow}slow 分别为快指针和慢指针,快指针表示遍历数组到达的下标位置,慢指针表示下一个不同元素要填入的下标位置,初始时两个指针都指向下标 11。
假设数组 \textit{nums}nums 的长度为 nn。将快指针 \textit{fast}fast 依次遍历从 11 到 n-1n−1 的每个位置,对于每个位置,如果 \textit{nums}[\textit{fast}] \ne \textit{nums}[\textit{fast}-1]nums[fast]
=nums[fast−1],说明 \textit{nums}[\textit{fast}]nums[fast] 和之前的元素都不同,因此将 \textit{nums}[\textit{fast}]nums[fast] 的值复制到 \textit{nums}[\textit{slow}]nums[slow],然后将 \textit{slow}slow 的值加 11,即指向下一个位置。
遍历结束之后,从 \textit{nums}[0]nums[0] 到 \textit{nums}[\textit{slow}-1]nums[slow−1] 的每个元素都不相同且包含原数组中的每个不同的元素,因此新的长度即为 \textit{slow}slow,返回 \textit{slow}slow 即可。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
};
复杂度分析
时间复杂度:O(n)O(n),其中 nn 是数组的长度。
快指针和慢指针最多各移动 nn 次。
空间复杂度:O(1)O(1)。只需要使用常数的额外空间。
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-tudo/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
solution2双指针
k记录不重复元素个数,同时作为更新数组时的下标
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size(), k = 1; // k记录不重复元素个数
if(n == 0) return 0;
for(auto& x: nums)
if(x != nums[k-1]) // 当前数不重复
nums[k++] = x; // k下标处元素更新为当前数,k+1
return k;
}
};
作者:nbgao
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array/solution/by-nbgao-w7na/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。