leetcode之Remove Duplicates from Sorted Array II
来源:互联网 发布:淘宝直通车开了就亏 编辑:程序博客网 时间:2024/06/08 09:06
题目:
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) { int start = 0; int end = 1; int size = nums.size(); if(size <= 2) return size; int cnt = 1; while(end < size) { if(nums[start] == nums[end]) { if(cnt < 2) { nums[++start] = nums[end++]; } else { end++; } cnt++; } else { nums[++start] = nums[end++]; cnt = 1; } } return start + 1; }};
0 0
- 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
- Leetcode: Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Leetcode Remove Duplicates from Sorted Array II
- Python装饰器
- Atom技巧
- OpenCV 的 GrabCut 可运行代码 及实例
- iOS Copy 关键字 的使用 原创 + 转载
- testNG入门学习(一)
- leetcode之Remove Duplicates from Sorted Array II
- linux知识复习一
- 堆和栈区别 生长方向和存储方向
- 求自定类型元素序列的中位数 PAT
- Xcode插件安装管理及常用插件
- POJ 2362
- 工作前最后一个寒假——刷题的日子
- java的package和import机制
- poj1679次小生成树