《leetcode》remove-duplicates-from-sorted-array-ii
来源:互联网 发布:国际航协2015数据 编辑:程序博客网 时间:2024/06/15 15:48
题目描述
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){//该数字出现的次数少于2 list.add(i); value++; hash.put(i,value); } } } for(int i=0;i<list.size();i++){ A[i]=list.get(i); } return list.size(); }}
阅读全文
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode - Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- LeetCode | Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【leetcode】Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Leetcode Remove Duplicates from Sorted Array II
- Scala---Akka Actor(三)Actor的持久化
- docker 时区问题
- Anagrams 归类 的 python 版本代码
- 图算法:Bellman-Ford算法和SPFA优化
- python二叉树深度 判断平衡树
- 《leetcode》remove-duplicates-from-sorted-array-ii
- 安装Xcode遇到的问题
- Ipython常用命令
- 配置Hercules的TCP/IP
- 小白入门---HTML5音乐播放器
- JavaScript-2-5:scroll、client、阻止冒泡
- 解决了电脑经常死机的问题
- 2012ICPC长春站 A Browsing History 【字符串】
- 【SQL Server学习笔记】18:对字符数据的处理