力扣链接:26. 删除有序数组中的重复项,难度等级:简单。
给你一个 非严格递增排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。
考虑 nums 的唯一元素的数量为 k ,你需要做以下事情确保你的题解可以被通过:
- 更改数组
nums,使nums的前k个元素包含唯一元素,并按照它们最初在nums中出现的顺序排列。nums的其余元素与nums的大小不重要。 - 返回
k。
示例 1:
输入: nums = [1,1,2]
输出: 2, nums = [1,2,_]
解释:
函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
示例 2:
输入: nums = [0,0,1,1,1,2,2,3,3,4]
输出: 5, nums = [0,1,2,3,4,_,_,_,_,_]
解释:
函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
约束:
1 <= nums.length <= 3 * 10^4-10^4 <= nums[i] <= 10^4nums已按 非严格递增 排列
提示 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 below for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements?
提示 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.
提示 3
Essentially, once an element is encountered, you simply need to bypass its duplicates and move on to the next unique element.
思路
站长 (张健): 👋
大家好!我是张健。
我深知学习算法和找到好工作之间的挑战。因此,除了这个项目,我个人还开发了
leader.me
— 这是程序员打造个人IP的终极平台,包含作品集、简历和博客等。
🚀 掌握算法是基础,而 leader.me 助您完美展示技能,顺利拿到 Offer!
立即前往 leader.me 打造你的程序员专属个人IP →
本题数组中的数值是排好序的,所以重复的数值一定是连续出现的。
要去掉其中的重复的数值,那么每一个不同于前一个数值的数都需要保留,否则就不保留。
因为要原地修改数据,所以需要涉及两个指针,一个指针走得快,一个指针走得慢。
快指针每次位置自动加1,慢指针指向当前需要修改位置。
“快慢指针技术”的模式
int slow = 0; // slow pointer
// ...
for (int fast = 1; fast < array_length; fast++) { // 本行是关键!
if (condition_1) {
// ...
continue; // 'continue' 比 'else' 好,因为 'else' 会引入更多的缩进空格!
}
if (condition_2) {
// ...
continue; // 'continue' 比 'else' 好
}
// condition_3
// ...
slow++;
// ...
}
return something;
复杂度
时间复杂度
O(N)
空间复杂度
O(1)
Python #
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
slow = 1
# nums = [0,0,1,1,1,2,2,3,3,4]
for fast in range(1, len(nums)):
if nums[fast] == nums[slow - 1]:
continue
nums[slow] = nums[fast]
slow += 1
return slow
Ruby #
# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
slow = 1
# nums = [0,0,1,1,1,2,2,3,3,4]
(1...nums.size).each do |fast|
if nums[fast] == nums[slow - 1]
next
end
nums[slow] = nums[fast]
slow += 1
end
slow
end
Java #
class Solution {
public int removeDuplicates(int[] nums) {
var slow = 1;
// nums = [0,0,1,1,1,2,2,3,3,4]
for (var fast = 1; fast < nums.length; fast++) {
if (nums[fast] != nums[slow - 1]) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}
其它语言
欢迎贡献代码到 LeetCode.blog GitHub -> 26. 删除有序数组中的重复项。感谢!打造你的开发者个人IP
🚀 掌握算法是成功的基石,而全方位展示你的才华则是获得垂青的关键。
我的另一个项目 leader.me —— 专为程序员打造的“全能型”个人品牌展示平台。三位一体(All-In-One)的职场利器:
- 📄 简历 + 作品集 + 博客: 将你的 GitHub 项目、技术心得与职场经历完美融合。
- 🌐 永久免费自定义域名: 支持绑定你自己的独立域名,且该功能永久免费。
- ✨ 顶级行业子域名: 提供
name.leader.me—— 极具职业含金量的专属域名。