题目描述

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

解析:借助hash的思想做该题目。

import java.util.*;
public class Solution {public int removeDuplicates(int[] A) {List<Integer> list = new ArrayList<Integer>();Map<Integer,Integer>  hash = new HashMap<>();for(int i:A){Integer value=hash.get(i);if(value==null){//第一次出现该数字hash.put(i,1);list.add(i);}else {if(value<2){//该数字出现的次数少于2list.add(i);value++;hash.put(i,value);}}}for(int i=0;i<list.size();i++){A[i]=list.get(i);}return list.size();}
}

《leetcode》remove-duplicates-from-sorted-array-ii相关推荐

  1. LeetCode 80. Remove Duplicates from Sorted Array II

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

  2. LeetCode之Remove Duplicates from Sorted Array II

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

  3. 【Leetcode】Remove Duplicates from Sorted Array II

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

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

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

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

  6. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

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

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

  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. [LeetCode] Remove Duplicates from Sorted Array II

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

  10. LeetCode之Remove Duplicates from Sorted Array

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

最新文章

  1. Linux UserSpace Back-Door、Rootkit SSH/PAM Backdoor Attack And Defensive Tchnology
  2. MVC中实现 加载更多...
  3. linux存储--inode与block详解(七)
  4. java 集合差运算_用一个参数的JAVA程序实现集合的交并差运算
  5. 任正非谈鸿蒙系统失误,谷歌也没想到会来的这么快,任正非谈鸿蒙:系统不难,生态快完善...
  6. c++实现时间轴,时间管理器
  7. C#:WinForm无边框窗体移动方法、模仿鼠标单击标题栏移动窗体位置
  8. mysql数据超10亿条,大型主键:超过10亿行MySQL + InnoDB?
  9. Tensorflow 迁移学习 识别中国军网、中国军视网Logo水印
  10. java Socket实现简单在线聊天(二)
  11. 360云台智能看家存储在哪_新品360智能摄像机云台变焦版,惊喜改变可不止一点点...
  12. 二维数组的最大联通子数组和
  13. Unity 移动 和 旋转 [小结]
  14. [Linux学习]更改默认启动图形界面或命令行
  15. 28个极简代码——python
  16. JJ斗地主记牌器java开发,【欢乐斗地主记牌器制作】遇到两个问题
  17. 《国产操作系统之银河麒麟》桌面环境
  18. ipad协议8033
  19. 首次!TinyML低功耗边缘侧机器学习技术论坛-亚洲分会来到中国!
  20. try{}里有一个return语句,那么紧跟在这个try后的finally{}里的代码会不会被执行,什么时候被执行,在return前还是后?

热门文章

  1. jQuery事件3——trigger触发事件
  2. [leetcode] 21.合并两个有序链表
  3. Android 自动向上滚动,android – Recyclerview在插入数据时自动向上滚动
  4. ironpython使用dictionary_在C#环境中动态调用IronPython脚本(一)
  5. 规格选择_止水螺杆规格及选择
  6. mysql 5.6.23 源码包安装报错_CentOS6.5_64bit下编译安装MySQL-5.6.23
  7. html5 背景拼贴,AI创建漂亮的无缝拼贴图案背景样式
  8. python之简单爬虫(爬取豆瓣出版社)
  9. RT-Thread FinSH控制台添加自定义msh命令原理
  10. 计算机模型机设计实验报告,基本模型机设计与实现 实验报告