题目:

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.

思路:给定一个有序vector,去年其中重复的数字后,并返回新vector的长度。

代码:

class Solution {
public:int removeDuplicates(vector<int>& nums) {if (nums.size()<2){return nums.size();//如果大小为1或者为空,则直接返回大小,不可能有重复}else{int a = *(nums.begin());//初始第一个数for (vector<int>::iterator iter = nums.begin() + 1; iter != nums.end();){if (a == *iter){//判断下一个数是否与前一个数相等if ((iter + 1) == nums.end()){//相等情况下,如果iter已经是最后一个数的位置,删除当前位置的数,并breakiter = nums.erase(iter);break;}else{//如果不是最后位置,则直接删除当前位置的数iter = nums.erase(iter);}}else{//如果不相等,则将a赋值为下一个数,iter递增 a = *iter;++iter;}}return nums.size();}}
};


LeetCode 26. Remove Duplicates from Sorted Array相关推荐

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

  2. leetCode #26 Remove Duplicates from Sorted Array

    删除相同数字 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int coun ...

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

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

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

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

  5. LeetCode 80. Remove Duplicates from Sorted Array II

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

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

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

  7. 【leetcode】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  9. LeetCode之Remove Duplicates from Sorted Array II

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

最新文章

  1. MySQL 数据库sql语句用关键字作为where条件进行筛选实例演示,mysql建表、查询字段为关键字解决方法
  2. Spring Boot 中的 RestTemplate 不好用?试试 Retrofit!
  3. linux的终端,网络虚拟终端,伪终端
  4. python中调用多进程加速处理文件
  5. IO系统性能之二:缓存和RAID如何提高IO
  6. 亲爱的SAP从业者们,烦请做个SAP知识学习种类的小调查
  7. f5源站获取http/https访问的真实源IP解决方案
  8. Docker 系列 _ 01_ 一念缘起
  9. Tensorflow学习笔记-基于LeNet5结构的ORL数据集人脸识别
  10. 无线网卡wifi无法启用
  11. 华为交换机如何清除console口密码
  12. 电脑管家卸载后留下的一个叫 电脑管家-安全注册 的进程,无法关闭。展开的服务是 qmbsrv
  13. 推广如何做网上引流?教你引流的底层逻辑
  14. ubuntu 安装kali_如何在Linux,Windows,Kali,Ubuntu,Mint和示例中安装和使用exiftool
  15. Verizon将基于从英特尔收购的技术打造新款机顶盒
  16. wsl2下安装lammps
  17. 2022-08-15 第四小组 修身课 学习笔记(every day)
  18. 跟我学ABAP/4-初识ABAP
  19. 小问题3--找不到Microsoft edge下载的扩展位置问题解决
  20. Linux0.11 文件打开open函数(五)

热门文章

  1. linux控制器错误码,ubuntu – 不频繁的输入/输出错误 – Linux服务器
  2. php 显示目录列表,php读取目录列表与文件列表的代码举例
  3. 计算机知识问答一站到底,一站到底的答题规则是什么
  4. git代码托管 · 操作举例: “git bash here ”(全程操作讲解) - git命令篇
  5. 垃圾模块清理工具小程序功能模块0.33
  6. python怎么用第三方库_python中第三方库的下载方法
  7. NP管理器v3.0.25 一键添加去除apk对话框等
  8. Biscuit – 超方便的多开浏览器[Win/macOS/Linux]
  9. wordpress主题webstack pro V2.0406 完美版
  10. [手把手教]discuzX2插件制作教程__最菜鸟级别的入门坎 【三】