删除排序数组重复元素,先来个简单的。

Remove Duplicates from Sorted Array

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

简单粗暴,重复一个则偏移量加1,遍历一次令A[i-k]=A[i]就可以了。看代码:

 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n) {
 4         if (n <= 1)
 5             return n;
 6         int k = 0;
 7         for (int i = 1; i < n; ++i) {
 8             if (A[i] == A[i - 1]) {
 9                 ++k;
10             } else if (k > 0) {
11                 A[i-k] = A[i];
12             }
13         }
14         return n - k;
15     }
16 };

题目加些条件:

Remove Duplicates from Sorted Array II

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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

允许重复出现两次。

LZ比较实在,只是老实的把以上代码的A[i]==A[i-1]的条件变成了i > 1 && A[i] ==  A[i-1] && A[i] == A[i-2]

然后果断受教育

Input:[1,1,1,2,2,3]

Output:[1,1,2,3]

Expected:[1,1,2,2,3]

分析原因:A[i-2]有可能不是原来的值了,因为是连续判断3个值,偏移只是偏移1个值,步长不对称。
天真地以为只有这个坑,于是加了个对k的约束,判断条件变成k != 1 && A[i] == A[i - 1] && A[i] == A[i - 2]
果断再次受教育

Input:[1,1,1,1]

Output:[1,1,1]

Expected:[1,1]

该偏移时不偏移了。
好吧,还是好好整理思路吧。
这里加的条件是允许2个,那如果条件逐渐变成允许3个、4个呢?
连写几个比较很明显是不行的,而且还要考虑各种情况,很复杂,设计一个通用的方案更靠谱。
LZ想到的是计数的方法了,记录上一次重复的次数,然后判断次数是否允许,允许则进行偏移,不允许则偏移量加1。
且看代码:
 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n) {
 4         int k = 0;
 5         int count = 1;
 6         for (int i = 1; i < n; ++i) {
 7             if (A[i] == A[i - 1]) {8                 count++;9                 if (count > 2) {
10                     k++;
11                     continue;
12                 }
13             } else {
14                 count = 1;
15             }
16             if (k > 0)
17                 A[i - k] = A[i];
18         }
19         return n - k;
20     }
21 };

转载于:https://www.cnblogs.com/flowerkzj/p/3619490.html

Leetcode OJ: Remove Duplicates from Sorted Array I/II相关推荐

  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 80. Remove Duplicates from Sorted Array II

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

  3. 【leetcode】Remove Duplicates from Sorted Array

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

  4. LeetCode之Remove Duplicates from Sorted Array II

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

  5. LeetCode之Remove Duplicates from Sorted Array

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

  6. LeetCode 26. Remove Duplicates from Sorted Array

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

  7. leetcode:2680 Remove Duplicates from Sorted Array 删除数组中的重复元素

    leetcode:26 对数组元素进行去重,使得原数组重复元素最多保留1个 限制: 我们不可以额外分配数组,必须保持空间复杂度为O(1) 这个并不难实现: class Solution(object) ...

  8. leetCode #26 Remove Duplicates from Sorted Array

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

  9. 【Leetcode】Remove Duplicates from Sorted Array II

    题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...

最新文章

  1. 将矩阵转为一行_矩阵与矩阵乘积简介
  2. 怎样建设WEB Cache
  3. Spring Security——org.springframework.security.oauth:spring-security-oauth2项目已过时解决方案
  4. 【第五组】头脑风暴+核心竞争力+NABCD+个人(用例+功能+技术说明书) 最后修改时间 2017.07.13...
  5. linux wptmp文件分析,wordpress上传图片提示“缺少临时文件夹”的解决方法
  6. linux之/usr/local/bin和/usr/bin区别
  7. java map集合 事务控制_对象回收过程?线程池执行过程? map原理?集合类关系?synchronized 和 volatile ? 同一个类的方法事务传播控制还有作用吗?java 锁...
  8. REST API 概念的简单介绍
  9. microservices kubernetes
  10. python官网的软件-Python编程软件 V3.9.0 官方最新版
  11. BZOJ5312 冒险 势能分析、线段树
  12. Navicat Premium 12.1.16.0 安装与激活(图文教程)
  13. 一张图解释清楚大数据技术架构,堪称阿里的核心机密
  14. 微软量子计算“天使梦”破碎,扬言的巨大胜利终究是一个“错误”
  15. win7 计算机名IPDNS修改,怎样改ip地址_怎样更换电脑ip地址-win7之家
  16. 机房消防报警系统及气体灭火防护的设计方法
  17. 单片机之时钟工作原理
  18. Django框架之MVT(1)
  19. flutter报错: [!] Automatically assigning platform `iOS` with version `8.0` on target `Runner` becaus
  20. 手赚网站搭建新手教程

热门文章

  1. 5.7 matlab数据插值与曲线拟合的比较
  2. 判断一个数组是否是另一个数组的子集
  3. 【Kaggle-MNIST之路】CNN结构再改进+交叉熵损失函数(六)
  4. Python Setuptools 升级(Upgrade)
  5. 爬虫为什么用Chrome?
  6. 讲讲排序(C++描述)
  7. for循环的一些困惑解决(字符串)
  8. P1865 A % B Problem (素数筛法,前缀和)
  9. dell r220服务器配置oracle linux 阵列卡,如何在Dell服务器PERC5/6阵列卡配置RAID
  10. python爬虫——利用BeautifulSoup4爬取糗事百科的段子