恢复刷题第二天,题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array/

26. Remove Duplicates from Sorted Array

  • Total Accepted: 157568
  • Total Submissions: 454924
  • Difficulty: Easy

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.

Subscribe to see which companies asked this question

题目很简单,给定一个排序好的数组,把相同的元素从原来的位置移除,并且返回一个不包含重复元素的长度即可。

同样是用了两种方法过的这道题,分别是unique和数组逐个比较的办法。

方法一:使用unique()函数:

int removeDuplicates(vector<int>& nums) {//unique()函数将重复的元素放到vector的尾部 然后返回指向第一个重复元素的迭代器//再用erase函数擦除从这个元素到最后元素的所有的元素nums.erase(unique(nums.begin(),nums.end()),nums.end());return nums.size();
}

方法二:数组元素逐个比较:

int removeDuplicates(vector<int>& nums)
{if (nums.empty()) return 0;int index = 0;for (int i = 1; i < nums.size(); i++){if (nums[index] != nums[i])nums[++index] = nums[i];}return index + 1;
}

每天一道题,保持新鲜感,就这样~

【LeetCode】马三来刷题之Remove Duplicates from Sorted Array相关推荐

  1. leetcode python3 简单题26. Remove Duplicates from Sorted Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十六题 (1)题目 英文: Given a sorted array nums, ...

  2. Remove Duplicates from Sorted Array II -- LeetCode

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

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

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

  4. 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++> 给出排序好的 ...

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

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

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

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

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

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

  8. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

  9. 2016.6.17——Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array 本题收获: 1."删除"数组中元素 2.数组输出 题目: Given a sorted array, rem ...

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

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

最新文章

  1. 通知提示SCPromptView
  2. VAE--就是AutoEncoder的编码输出服从正态分布
  3. 《Cisco IOS XR技术精要》一4.4 理解二级提交模型
  4. python从官网安装库函数的方法;
  5. MySQL不能插入中文字符及中文字符乱码问题
  6. defer与async的认识
  7. #35 string(缩点+动态规划)
  8. (74)FPGA面试题-Verilog序列发生器,产生 10110 的序列
  9. 95-110-022-源码-Env-LocalStreamEnvironment
  10. 查询 加载时间过长添加提示信息
  11. 大数据挖掘与分析——用八爪鱼爬虫采集百度搜索内容
  12. C++复数运算符重载,复数开平方
  13. mysql三表联查sql语句_SQL三表连接查询与集合的并、交、差运算查询
  14. Git 工具 - 储藏(Stashing) git stash 暂存现在工作区的内容
  15. 怎么在拦截器里接收json对象_九型人格分析:怎么挑选适合的爱人和结婚对象,藏在他的性格里...
  16. iVX开发过程整理的常见问题与回答(二)
  17. 王柏北邮计算机副院长,“中国电信上海研究院—北京邮电大学计算机学院战略合作框架协议”签约仪式隆重举行...
  18. 《我为什么熬夜?》系列之 倚天屠龙记
  19. 你想制作一款属于自己的游戏吗?
  20. 十四、Nginx--rewrite规则

热门文章

  1. JS中监听事件的写法
  2. 微信公众号开发--实现扫码关注公众号自动登录网站
  3. idea常用的搜索方式
  4. java中instant_Instant
  5. 算法-图(3)用顶点表示活动的网络(AOV网络)Activity On Vertex NetWork
  6. LTE中RB和RE、REG、CCE的定义
  7. 杂评 360和腾讯之争
  8. 《四》大话 TypeScript 泛型
  9. 二维码名片的生成与读取
  10. 生物光子学 19节 加州大学戴维斯分校