Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素

考察数组的基本操作:

class Solution {public int removeDuplicates(int[] nums) {if (nums==null || nums.length==0)return 0;int index = 1;for(int i =1; i<nums.length; i++){if(nums[i]!=nums[i-1]){nums[index] = nums[i];index++;}}return index;}public static void main(String[] args) {int[] arr = { 1, 2, 2, 3, 3 };arr = removeDuplicates(arr);System.out.println(arr.length);
}
}

Remove Duplicates from Sorted Array II (Java)

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

最多允许两个重复,输出结果数组。

解法1:当counter是2时,就直接跳过即可,否则说明元素出现次数没有超,继续放入结果数组,若遇到新元素则重置counter。总体算法只需要扫描一次数组,所以时间上是O(n),空间上只需要维护一个index和counter,所以是O(1)。

public int removeDuplicates(int[] A) {if(A==null || A.length==0)return 0;int idx = 0;int count = 0;for(int i=1;i<A.length;i++){if(A[i]==A[i-1]){count++;if(count>=3)continue;}else{count = 1;}A[idx++]=A[i];}return idx;
}

解法二:

public class Solution {public int removeDuplicates(int[] A) {if (A.length <= 2)return A.length;int prev = 1; // point to previousint curr = 2; // point to currentwhile (curr < A.length) {if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {curr++;} else {prev++;A[prev] = A[curr];curr++;}}return prev + 1;}
}

转载于:https://www.cnblogs.com/DLlearning/p/7879711.html

leetCode-数组: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 OJ: Remove Duplicates from Sorted Array I/II

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

  3. LeetCode 80. Remove Duplicates from Sorted Array II

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

  4. LeetCode之Remove Duplicates from Sorted Array

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

  5. 【leetcode】Remove Duplicates from Sorted Array

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

  6. LeetCode之Remove Duplicates from Sorted Array II

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

  7. LeetCode 26. Remove Duplicates from Sorted Array

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

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

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

  9. leetCode #26 Remove Duplicates from Sorted Array

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

  10. 【Leetcode】Remove Duplicates from Sorted Array II

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

最新文章

  1. idea配置echache.xml报错Cannot resolve file 'ehcache.xsd'
  2. Python 自动化-pywinauto库定位树结构控件里的树节点实例演示
  3. [hypervisor]-AArch64 (hypervisor)Virtualization学习笔记
  4. window10最全win键组合技巧(win10快捷键)
  5. 2019牛客暑期多校训练营(第六场)
  6. 控制反转-依赖倒置-依赖注入
  7. java学习(99):车站卖票问题
  8. MySQL关键字EXPLAIN的用法及其案例
  9. dj鲜生-22-模板抽离-列表页详情页模板的抽离-base_detail_list
  10. [转]Oracle_ProC编程
  11. 电气自动化c语言用什么软件,电气自动化需要哪些高级语言?
  12. 自己的包增加为第三方包,使用Eclipse环境报Unresolved import错误(pycharm可用正常引用)...
  13. JAVA实现生成GIF动态图加文字(完整版无License带锯齿优化处理)
  14. 详解:什么是VXLAN?
  15. 【报名开启】2021年博客之星总评选,属于你的年终表彰
  16. Qt QStringLiteral
  17. java实现俄罗斯方块项目
  18. 构建整车七自由度模型
  19. 【PostgreSQL】数据表的增删改查
  20. 徕卡DNA03水准仪USB驱动

热门文章

  1. 准备创业或刚创业的朋友必读
  2. 【SpringBoot】 logback 日志的集成
  3. CIO都在用的数据可视化软件
  4. 案例解析|政府信息化的BI建设应用
  5. 论制造业的报表开发项目需求
  6. ras私钥c#转java_RSA密钥,JAVA与.NET之间转换
  7. python中函数用法教程_Python中zip()函数用法实例教程
  8. 计算机算法设计与分析 最长子序列
  9. Tr A 矩阵快速幂
  10. 2019 蓝桥杯省赛 B 组模拟赛(一) J. 程序设计:蒜厂年会 环形连续子序列求和问题