Remove Duplicates from Sorted Array II
2015-08-25 18:37
411 查看
题目简述
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
解题思路
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ d = {} l = len(nums) if l < 2: return l i = 0 while True: if i >= l: break if nums[i] not in d: d[nums[i]] = 1 else: d[nums[i]] += 1 if d[nums[i]] > 2: nums.remove(nums[i]) l -= 1 continue i += 1 return l
相关文章推荐
- sass
- JavaScript学习笔记
- Vm+linux挂载U盘和SD卡的说明
- Google APAC 2016 University Graduates Test Round A解题报告
- POJ-Round Numbers-组合数学
- i5
- HDU 2578 二分
- opencv + Qt + codeblocks
- and5.1PowerManagerService深入分析(一) PMS的初始化以及低功耗模式
- ProtocolBuf协议说明
- Python 并行分布式框架:Celery 超详细介绍
- android数据库事务
- 探索javascript中函数的执行顺序
- HDU4336——期望+状态压缩DP——Card Collector
- jquery或者js对html控件的处理汇总
- JAVA中extends 与implements区别
- C++ 学习笔记
- tomcat配置集群负载均衡
- LWIP-工业以太网交换机 SNMP功能与SStool功能完成笔记
- jQuery on重复邦定事件