题目:

Given a sorted array, 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 in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

题解:

  感觉这个题是真没什么好解析的。。。

Solution 1()

class Solution {
public:int removeDuplicates(vector<int>& nums) {int n = nums.size();if(n < 2)return n;int index = 0;for(int i = 1; i < n; ++i)if(nums[index] != nums[i])nums[++index] = nums[i];return index + 1;}
};

Solution 2()

class Solution {
public:int removeDuplicates(vector<int>& nums) {int n = nums.size();int count = 0;for(int i = 1; i < n; i++){if(A[i] == A[i-1]) count++;else A[i-count] = A[i];}return n-count;}
};

Solution 3

class Solution {
public:int removeDuplicates(vector<int>& nums) {int len = nums.size();if (len <= 0) {return 0;}int prev = 0, cur = 0;while (cur < len) {if (nums[cur] == nums[prev]) {++cur;} else if (cur != prev + 1){nums[++prev] = nums[cur++];} else {++prev;++cur;}}return prev + 1;}
};

为了避免多余的复制操作,可以扩展使用,比如类数组,当数组没有重复对象时,无需复制操作。而之前的两个解答将会依次复制。

转载于:https://www.cnblogs.com/Atanisi/p/6752217.html

【LeetCode】026. Remove Duplicates from Sorted Array相关推荐

  1. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  2. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  3. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  4. 【leetcode】33. Search in Rotated Sorted Array

    题目如下: 解题思路:题目要求时间复杂度是O(log n),而且数组也是有序的,那么可以考虑采用二分查找法.那么解题的关键就是找出转折点,找到了转折点后,把数组拆分成两段,再分别用二分查找,即可得到答 ...

  5. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  6. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  7. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

  8. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  9. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  10. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

最新文章

  1. java 初识对象和对象引用的关系
  2. PAT题解-1118. Birds in Forest (25)-(并查集模板题)
  3. Excel:如何使用函数实现多表多条件汇总求和
  4. java垃圾_Java很垃圾吗?
  5. Python_XPath
  6. [原]ASP.Net常用功能整理--生成图片的缩略图
  7. mysql驱动加载原理_老调重弹:JDBC系列 之 lt;驱动载入原理全面解析gt;
  8. pandas 索引去重_Pandas 同元素多列去重的实例
  9. CHM乱码解决方案!
  10. 获取对象的key_玩转 SpringBoot2.x 之缓存对象
  11. linux 文件句柄 sock,linux socket句柄
  12. linux里的tree 命令,Linux中tree命令起什么作用呢?
  13. 钉钉群机器人关键词自动回复_自动化运维平台Spug测试
  14. 写论文之LaTex-安装texlive
  15. Java使用ucanaccess连接Access数据库,报错:UCAExc:::4.0.0 user lacks privilege or object not found: XXXXXX
  16. 大数据学习基础知识总纲
  17. C++程序设计课程主页-2015级
  18. WIN 7 系统 问题记录
  19. 网易邮箱阻止一次非法访问解除方法
  20. 运筹学研究者关注的Github和CSDN账号

热门文章

  1. 【渝粤题库】 陕西师范大学 210021 学前儿童健康教育 作业(专升本)
  2. 修改初始Manager QuerySets,重写Manager.get_query_set()函数之后,发现并没有按照我们指定的方法执行。...
  3. 2018年湘潭大学程序设计竞赛 F maze
  4. react-native 开发在Android模拟器上运行
  5. CJOI 05新年好 (最短路+枚举)
  6. linux 统计TCP 连接各状态总数
  7. web前端入门:CSS 样式书写规范
  8. 微信 oauth授权2
  9. Quartz.net官方开发指南系列篇
  10. 记一次内存无法回收导致频繁fullgc机器假死的思路