[leetcode] Remove Duplicates from Sorted Array II
来源:互联网 发布:linux 报文发送工具 编辑:程序博客网 时间:2024/09/21 06:35
From : https://leetcode.com/problems/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.
Solution :
class Solution {public: int removeDuplicates(vector<int>& nums) { int len, p=0; if((len = nums.size()) <= 2) return len; for(int i=0; i<len;) { if(i < len-1 && nums[i]==nums[i+1]) { int v = nums[i]; nums[p++] = nums[i++]; nums[p++] = nums[i++]; while(v == nums[i]) i++; } else { nums[p++] = nums[i++]; } } return p; }};
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
- BFS遍历图代码
- MyEclipse2015 如何隐藏没用到的Server(MyEclipse Derby, MyEclipse Tomcat v7.0)
- 黑马程序员--Objective-C——面向对象
- hdu2710 筛选法求素数
- Android系统各版本及代号
- [leetcode] Remove Duplicates from Sorted Array II
- 你可能没注意的CSS单位
- PAT 数据结构 08-排序3. Talent and Virtue (25)
- 使用ObjectAnimator开发打开、关闭书本动画
- 了解 uri, content provide,包括 data android:host,android:scheme
- POJ——1458最长公共子序列
- Java中map学习笔记(一)
- 两种Linux下非交互式命令的实现
- 【独立开发者er Cocos2d-x实战 007】使用Cocos2dx UserDefault.xml