Leetcode106: Remove Duplicates from Sorted Array II
2015-10-19 17:03
459 查看
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums =
Your function should return length =
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,
2and
3.
It doesn't matter what you leave beyond the new length.
class Solution { public: int removeDuplicates(vector<int>& nums) { int k = 0; for(int i = 0; i < nums.size(); i++) { if(i <= 1) nums[k++] = nums[i]; else { if(nums[i] == nums[k-2]) { } else { nums[k++] = nums[i]; } } } return k; } };
相关文章推荐
- 程序动态分析工具调研
- GridLayout与TableLayout布局
- 推荐系统需要考虑的三部分
- 修改Linux的系统时间
- 数据归一化汇总
- 用状态机实现键盘消抖
- linux安装jdk和tomcat
- eclipse中各种颜色和背景的设置
- Python快速教程
- 《剑指Offer》面试题:用两个栈来实现一个队列
- iostat 磁盘性能统计
- Leetcode Spiral Matrix
- 第七周项目1--建立顺序环形队列算法库
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- Java版的Redis
- Google Guava Collections 使用介绍
- java jdbc
- Uva821-Floyd
- Linux的概念与体系
- 学习struts 浅要的理解 希望大牛指导