Remove Duplicates from Sorted Array II
来源:互联网 发布:java数据库代码生成器 编辑:程序博客网 时间:2024/06/10 16:37
Remove Duplicates from Sorted Array II
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 {public: int removeDuplicates(vector<int>& nums) { if (nums.empty()) return 0; int curpos=1; int times=1; int cur=nums[0]; for (int i=1; i<nums.size(); i++) { if (nums[i]!=cur) { cur=nums[i]; nums[curpos++]=cur; times=1; } else { if (times==1) { times++; nums[curpos++]=cur; } else { times++; continue; } } } return curpos; }};
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- 工具类 将<InputStream> 字节输出流--->转换成-->String类型
- JDK动态代理
- poj 1953 World Cup Noise
- Android 响应式编程 RxJava2 完全解析
- csv文件导入Neo4j(包括结点和关系的导入)
- Remove Duplicates from Sorted Array II
- 系统恢复技术
- Struts(7)Struts的DispatchAction(分派Action)和全局跳转
- 《逆向工程核心原理》<03-27> 代码注入
- NYOJ-399整除个数
- 排序问题二(简单选择排序)
- Codeforces 798C Mike and gcd problem【思维+贪心】好题!
- 底部一直在最底部不管内容多少
- hive错误记录