今天发现一个好东西--leetcode的course ,虽然没有付费的内容会比较少,不过也很不错了。

第一篇的string讲的是两点法(Two-pointer technique),也就是数据结构课本里常用的快慢指针。原理很简单,但是题目里要用到的stl有点忘了。。

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.

两点法的经典应用除了实例里说的反转,还有除重,代码很简单

class Solution {
public:int removeDuplicates(vector<int>& nums) {if(nums.size() <= 1)return nums.size();vector<int>::iterator fast = nums.begin()+1;vector<int>::iterator slow = nums.begin();while(fast != nums.end()){if(*fast == *slow){nums.erase(fast);}else{++fast;++slow;}}return nums.size();}
};

leetcode能通过,用vs2015不知为何会运行出错。要注意的是vector的erase清楚之后后续元素会往前移动。

转载于:https://www.cnblogs.com/tonychen-tobeTopCoder/p/5149889.html

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

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

  3. leetCode #26 Remove Duplicates from Sorted Array

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

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

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

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

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

  6. LeetCode 80. Remove Duplicates from Sorted Array II

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

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

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

  8. 【leetcode】Remove Duplicates from Sorted Array

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

  9. 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 ...

  10. LeetCode之Remove Duplicates from Sorted Array II

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

最新文章

  1. 使用Xcode修改iOS项目工程名和路径名
  2. java和python根据对象某一个属性排序
  3. C++ Primer 第9章 顺序容器 第一次学习笔记
  4. MapReduce概述
  5. Centos/Red Hat6.8 安装、配置、启动Gitlab (内网环境)
  6. 开源界也要注意,Apache 基金会与 GitHub 都受美国法律约束
  7. 计算机用户改路径,如何更改win7 Users(用户文件夹)文件存放位置?
  8. 实现WEB打印的几种方法
  9. 用java 解密pdf_Java 加密、解密PDF文档(示例代码)
  10. 单应性矩阵的理解及求解
  11. 微信服务器IP地址清单
  12. MindSpore布道师招募计划,开启AI的信仰之跃
  13. python如何控制鼠标键盘_Python如何控制鼠标键盘
  14. 计算机学院篮球队介绍,2018年校级篮球联赛计算机与信息学院篮球队专访
  15. 陕西师范大学,我终于来了!
  16. Python爬虫:逆向分析某酷音乐请求参数
  17. 【JavaSE】String类详解含面试经典
  18. 【狂神说JAVA】SpringMVC笔记
  19. TP4054/TP4056/TP4057锂电充电芯片之灯不灭处理方法
  20. 正态分布对数据分析很重要!

热门文章

  1. profile asp.net technology membership
  2. 有关Spring 3.0的发布
  3. 如何在 Linux 中创建一个共享目录
  4. HDU2255 奔小康赚大钱(km模板题)
  5. 利用tftp和nfs下载文件到开发板
  6. jquery获取元素的值,获取当前对象的父对象等等
  7. 6/5 补瓷砖,购浴缸
  8. C++内存对象大会战
  9. Dubbo的总体架构
  10. aspose-cells 表合并