1. class Solution {
  2. public:
  3. int removeDuplicates(int A[], int n) {
  4. int *s=&A[],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历相同的
  5. int i,flag=,j=n;
  6. for(i=;i<n;i++){
  7. e++;
  8. if(*s!=*e||(*s==*e&&flag==) ){ //新的数字,直接加复制到s处
  9. if(*s==*e)
  10. flag=;
  11. else
  12. flag=;
  13. s++;
  14. *s=*e;
  15. }
  16. else
  17. j--;
  18. }
  19. return j;
  20. }
  21.  
  22. };

题意:提供一个整型数组,和该数组内有n个元素,数组已经有序。相同的数字不允许>2个,要么1个,要么2个。修改原数组,并返回剩下的元素个数。

思路:增加一个标志flag=0,遇到多个相同的数字时,第一个正常处理,在第2个数字的处理时将标志改变一下为非零,那么后面相同的就可以去掉了。

LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  2. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

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

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

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

  4. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  5. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

  6. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  7. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  8. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  9. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

随机推荐

  1. Android 全局错误管理

    package com.wlwl.yiyuan; import java.io.File; import java.io.PrintWriter; import java.io.StringWrite ...

  2. CodeForces 131D【图特性+BFS】

    题意: 只有一个环,然后环都是0(环缩点相当于树的根),然后其余的输出到根的距离 思路: 可以从度为1的 开始搜 把那些分支全标记掉,然后再取没有标记掉的,BFS一下搞出距离. 具体这个标记: 倒着搜 ...

  3. 20个问题(状压dp)

    20个问题(状压dp) 有n(<=128)个物体,m(<=11)个特征.每个物体用一个m位01串表示,表示每个特征是具备还是不具备.我在心里想一个物体,由你来猜.你每次可以询问一个特征,然 ...

  4. Winform下极简后台异步工作教程

    BackgroundWorker worker = new BackgroundWorker();//定义后台进程 worker.WorkerReportsProgress = true;//允许报告 ...

  5. vue中一些常见错误

    一:在抽取路由模块时路径没有更改过来 二:跨域的问题

  6. Composite模式(组合设计模式)

    Composite 设计模式? 在计算机的文件系统中,有"文件夹"的概念(在有些操作系统(Linux操作系统)中,也称为"目录").文件夹里面既可以放入文件,也 ...

  7. HDU6438:Buy and Resell(贪心+数据结构)

    题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少 分析:对每一个元素产生的贡献可以先计算 ...

  8. BestCoder Round #86 1002

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5805 题意:删除数列中一个数,找出相邻之差绝对值最大,求依次删除最大值的和 解法:删除边缘位置的数字需要注 ...

  9. dpkg dependency problems prevent configuration

    dpkg: dependency problems prevent configuration of cr3: cr3 depends on libpng12-0 (>= 1.2.13-4); ...

  10. 014 Longest Common Prefix 查找字符串数组中最长的公共前缀字符串

    编写一个函数来查找字符串数组中最长的公共前缀字符串. 详见:https://leetcode.com/problems/longest-common-prefix/description/ 实现语言: ...