【leetcode 数组】Remove Duplicates from Sorted Array II
来源:互联网 发布:工地临时用电计算软件 编辑:程序博客网 时间:2024/04/23 23:29
题目:
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]
.
分析:
与上一篇Remove Duplicates from Sorted Array不同,题目要求允许每个数最多重复两次,则可以设置一个标志位flag用于标记
代码参考:
class Solution {public: int removeDuplicates(int A[], int n) { if(n==0) return 0; int flag=1; //标记变量,表示重复的个数 int index=0; for(int i=1;i<n;i++) { if(A[index]==A[i] && flag==1) { A[++index]=A[i]; flag=2;} else if(A[index]!=A[i]) { A[++index]=A[i]; flag=1;} } return index+1; //下标加1 表个数 }};
0 0
- LeetCode--Remove Duplicates from Sorted Array II (数组操作)
- 【leetcode 数组】Remove Duplicates from Sorted Array II
- [leetcode]【数组】80. Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode - Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- LeetCode | Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【leetcode】Remove Duplicates from Sorted Array II
- Block实战应用之button点击事件和Alert弹出框
- 关于“非法使用精神控制技术和生理武器罪”的刑法增修案(正式建议稿)
- Android不刷机下的app2sd方法(dex cache占空间解决篇)
- linux vim,vi编辑器的基础
- OpenCV中的origin成员
- 【leetcode 数组】Remove Duplicates from Sorted Array II
- rac下/tmp/bootstrap权限问题
- 第九周项目3.4 输出点阵4
- java转义字符分类
- Android自定义View之Gallery实现3D图片播放器
- 关于 C++ 框架、库和资源的一些汇总列表
- 求多边形面积(计算几何)
- cin.getline()和cin.get()的区别
- systemd for Administrators, Part 1