Remove Duplicates from Sorted Array II
来源:互联网 发布:淘宝网电话 编辑:程序博客网 时间:2024/06/16 04:58
自己搞定
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]
.
C++
class Solution {public: int removeDuplicates(int A[], int n) { if(n<3) return n; int index = 1, count = 1; for(int i=1;i<n;i++){ if(A[i]==A[index-1] && count<2){ A[index++] = A[i]; count += 1; } if(A[i]!=A[index-1]){ A[index++] = A[i]; count = 1; } } return index; }};
summary:
数组可以in-place的替换,这个一定要掌握。 相当于前面是一个新数组,后面是一个数组。
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
- redis sort命令用法
- mysql游标和临时表
- 一个拖延者的自我救赎
- xmpp在企业通信的应用
- javascript数组的常用方法总结
- Remove Duplicates from Sorted Array II
- Linux系统之文件目录结构
- 对IDE依赖的一些看法
- querySelectorAll 方法相比 getElementsBy 系列方法有什么区别
- 栈和队列实验
- Android禁止横竖屏切换或禁止横竖屏切换时重启Activity
- 【记录】数控程序的指令代码---标准G代码与标准M代码
- fflush(stdin)和fflush(stdout)函数
- 2014-11-6Android学习------SurfaceView的框架源码