LintCode-删除排序数组中的重复数字 II
2015-10-23 12:51
453 查看
容易 删除排序数组中的重复数字
II
查看运行结果
30%通过
跟进“删除重复数字”:
如果可以允许出现两次重复将如何处理?
您在真实的面试中是否遇到过这个题?
Yes
样例
给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]。
public class Solution { /** * @param A: a array of integers * @return : return an integer */ public int removeDuplicates(int[] nums) { HashMap<Integer , Integer> map = new HashMap<Integer , Integer>(); int n=0; for(int i=0;i<nums.length;i++){ if(map.get(nums[i])==null){ map.put(nums[i], 1); nums[n++]=nums[i]; }else{ if(map.get(nums[i])<2){ map.put(nums[i], map.get(nums[i])+1); nums[n++]=nums[i]; } } } return n; } }
相关文章推荐
- LintCode-最大数
- Subtree
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- LintCode --invert-binary-tree(翻转二叉树)
- LintCode --find-the-missing-number(寻找缺失的数)
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- LintCode 翻转二叉树
- LintCode 寻找缺失的数
- Lintcode 二叉树的后序遍历
- Lintcode 二叉树中序遍历
- Lintcode 二叉树前序遍历
- Lintcode 尾部零的个数
- Lintcode 比较字符串
- Lintcode 主元素
- Lintcode解题笔记 - 岛屿的个数
- LintCode -合并两个排序链表
- 20150708 lintcode 总结 Binary Tree Maximum Path Sum Show result *****
- 20150708 lintcode 总结 Minimum Path Sum
- 20150708 lintcode 总结 Binary Search Tree Iterator