题目:

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 1122 and 3. It doesn't matter what you leave beyond the new length.

题解:

Solution 1

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int k = ;
int n = nums.size();
if(n < )
return n;
int index = , cnt = ;
for(int i = ; i < n; ++i){
if(nums[i] != nums[index]){
cnt = ;
nums[++index] = nums[i];
} else if(++cnt <= k){
nums[++index] = nums[i];
}
}
return index + ;
}
};

Solution 2

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int k = ;
int n = nums.size();
if(n < k)
return n;
int index = k;
for(int i = k; i < n; ++i){
if(nums[i] != nums[index - k])
nums[index++] = nums[i];
}
return index;
}
};

此解与Remove Duplicates from Sorted Array相似

最新文章

  1. MergeRecord_C++中map的使用
  2. chrome浏览器渲染白屏问题剖析
  3. 跨域http请求
  4. CEF3开发者系列之进程间消息传递
  5. [poj3321]Apple Tree(dfs序+树状数组)
  6. 英語版Windows Server 2012 R2を日本語化する手順
  7. android studio问题-ICCP:Not recognizing known sRGB profile
  8. SuperGridControl 使用小技巧
  9. lucene、lucene.NET详细使用与优化详解
  10. N对括号的合法组合
  11. Oracle数据库之视图与索引
  12. UItableViewCell上的button点击无响应的办法
  13. PAT (Advanced Level) 1107. Social Clusters (30)
  14. GIt -- git push 远程分支老是需要重新输入公钥密码问题处理?
  15. Difference Between Git and SVN
  16. 安装redis及异常处理
  17. S 实现精确加减乘除
  18. HTTP 总结
  19. 【Java】 剑指offer(7) 二叉树的下一个结点
  20. QTextCodec 类

热门文章

  1. Dbvisualizer 连接oracle数据库
  2. BZOJ1791: [Ioi2008]Island 岛屿
  3. mysql(root用户密码设置)
  4. python+NLTK 自然语言学习处理五:词典资源
  5. MySQL删除相同前缀的表,修改某个库的存储引擎
  6. memcached 不同客户端的问题
  7. 磁盘检测SMART工具
  8. CentOS6安装DaoCloud加速器
  9. HDU - 1272 小希的迷宫 【并查集】
  10. flask初次搭建rest服务笔记