leetcode---Remove Duplicates from Sorted Array II
2016-07-12 23:00
507 查看
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.
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 len = nums.size(); if(len == 0) return 0; int lastIndex = 0; int cnt = 0; for(int i=0; i<len;) { while(i < len && nums[i] == nums[lastIndex]) { cnt++; i++; } while(cnt > 2) { nums[lastIndex++] = -1000000; cnt--; } lastIndex = i; cnt = 0; } vector<int> tmp; for(int i=0; i<len; i++) if(nums[i] != -1000000) tmp.push_back(nums[i]); nums = tmp; return nums.size(); } };
相关文章推荐
- 装配bean
- HDU 4507 吉哥系列故事——恨7不成妻
- C语言学习-分支结构
- MyBatis
- 四种常用的标准自定义View方法(上)
- flume
- java基础之----匿名内部类
- Retrofit 一种适用于Android和Java的类型安全的http客户端
- Android Studio官方文档之创建可调整大小的位图
- Extended WPF toolkit 安装使用指南 —— V1.6版本【2016-06-19】版本
- 第十八天:规划风险管理和识别风险
- linux内核栈和用户栈(二)
- 第十八天:规划风险管理和识别风险
- μC/OS-II任务管理中的主要函数解析
- ECSHOP因PHP版本导致的错误
- Spring 配置笔记
- “不务正业”玩投资的联通能翻身吗?
- 困扰一周多的问题总算解决了
- POJ2975 Nim
- android服务之录音功能