Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

  1. Given nums = [1,1,2],
  2.  
  3. Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
  4.  
  5. It doesn't matter what you leave beyond the returned length.

Example 2:

  1. Given nums = [0,0,1,1,1,2,2,3,3,4],
  2.  
  3. Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
  4.  
  5. It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

  1. // nums is passed in by reference. (i.e., without making a copy)
  2. int len = removeDuplicates(nums);
  3.  
  4. // any modification to nums in your function would be known by the caller.
  5. // using the length returned by your function, it prints the first len elements.
  6. for (int i = 0; i < len; i++) {
  7.     print(nums[i]);
  8. }

这道题要我们从有序数组中去除重复项,和之前那道 Remove Duplicates from Sorted List 的题很类似,但是要简单一些,因为毕竟数组的值可以通过下标直接访问,而链表不行。那么这道题的解题思路是使用快慢指针来记录遍历的坐标,最开始时两个指针都指向第一个数字,如果两个指针指的数字相同,则快指针向前走一步,如果不同,则两个指针都向前走一步,这样当快指针走完整个数组后,慢指针当前的坐标加1就是数组中不同数字的个数,代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int removeDuplicates(vector<int>& nums) {
  4. int pre = , cur = , n = nums.size();
  5. while (cur < n) {
  6. if (nums[pre] == nums[cur]) ++cur;
  7. else nums[++pre] = nums[cur++];
  8. }
  9. return nums.empty() ? : (pre + );
  10. }
  11. };

我们也可以用 for 循环来写,这里的j就是上面解法中的 pre,i就是 cur,所以本质上都是一样的,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int removeDuplicates(vector<int>& nums) {
  4. int j = , n = nums.size();
  5. for (int i = ; i < n; ++i) {
  6. if (nums[i] != nums[j]) nums[++j] = nums[i];
  7. }
  8. return nums.empty() ? : (j + );
  9. }
  10. };

这里也可以换一种写法,用变量i表示当前覆盖到到位置,由于不能有重复数字,则只需要用当前数字 num 跟上一个覆盖到到数字 nums[i-1] 做个比较,只要 num 大,则一定不会有重复(前提是数组必须有序),参见代码如下:

解法三:

  1. class Solution {
  2. public:
  3. int removeDuplicates(vector<int>& nums) {
  4. int i = ;
  5. for (int num : nums) {
  6. if (i < || num > nums[i - ]) {
  7. nums[i++] = num;
  8. }
  9. }
  10. return i;
  11. }
  12. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/26

类似题目:

Remove Duplicates from Sorted List

Remove Duplicates from Sorted List II

Remove Duplicates from Sorted Array II

Remove Element

类似题目:

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

https://leetcode.com/problems/remove-duplicates-from-sorted-array/discuss/11757/My-Solution-%3A-Time-O(n)-Space-O(1)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项的更多相关文章

  1. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  2. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  3. [LeetCode每日一题]80. 删除有序数组中的重复项 II

    [LeetCode每日一题]80. 删除有序数组中的重复项 II 问题 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使用额外 ...

  4. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  5. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  6. [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  7. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  8. 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点

    给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...

  9. LeetCode(26): 删除排序数组中的重复项

    Easy! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...

随机推荐

  1. ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态

    原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...

  2. 移动WEB开发之viewport

    问题: 在codepen上写了一个响应式页面,调试的时候没有问题.结果放到网站上,在手机上打开之后竟然和在电脑中的布局是一样的.         查阅资料之后知道响应式布局应该有这样一句话:<m ...

  3. Java进击C#——应用开发之WinForm开发

    本章简言 上一章笔者介绍了关于WinForm环境.这一章笔者将继续讲WinForm.只不过更加的面向开发了.事实就是在学习工具箱里面的控件.对于WinForm开发来讲,企业对他的要求并没有那么高.但是 ...

  4. 一位同事对 Rafy 框架的一些建议及我的回复

    下面是一位同事对当前的产品开发框架提出的一些建议,以及我的回复.我觉得一些问题提得有一定的代表性,在征得本人同意后,将本邮件发布在博客中. 同时,也非常希望对框架.产品有好的建议的小伙伴,都可以给我发 ...

  5. 【C#进阶系列】27 I/O限制的异步操作

    上一章讲到了用线程池,任务,并行类的函数,PLINQ等各种方式进行基于线程池的计算限制异步操作. 而本章讲的是如何异步执行I/O限制操作,允许将任务交给硬件设备来处理,期间完全不占用线程和CPU资源. ...

  6. php分页原理

    <?php //包含连接MySQL的文件 include "conn.php"; //分页的相关变量 $pagesize = 5; //每页显示条数 //获取地址栏中传递的p ...

  7. CXF:根据werservice代码生成WSDL(转)

    原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...

  8. MyEclipse相关部署问题

    部署Tomcat如果用MyEclipse自动部署方式很有可能出现一个问题: 服务器关联的这个项目却关联到其他的项目上,导致运行时还在运行以前的项目 解决方法: 将状态提示栏的service里的tomc ...

  9. 总结js常用函数和常用技巧(持续更新)

    学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...

  10. H5 meta小结

    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1, ...