[leetcode 80, Medium] Remove Duplicates from Sorted Array II
来源:互联网 发布:seo 刷关键词排名工具 编辑:程序博客网 时间:2024/06/05 17:28
Problem:
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.
Analysis:
Solutions:
C++:
int removeDuplicates(vector<int>& nums) { if(nums.size() <= 2) return nums.size(); for(vector<int>::iterator it = nums.begin(); it != nums.end();) { if(it == nums.begin() || (it != nums.end() - 1 && *it != *(it - 1))) { if(*it != *(it + 1)) { ++it; continue; } else { ++it; if(*it != *(it + 1)) { ++it; continue; } else { for(++it; it != nums.end() && *it == *(it - 1);) { it = nums.erase(it); } } } } else ++it; } return nums.size(); }Java:
Python:
0 0
- [leetcode 80, Medium] Remove Duplicates from Sorted Array II
- LeetCode-80-Remove Duplicates from Sorted Array II(递归)-Medium
- Leetcode 80. Remove Duplicates from Sorted Array II (Medium) (cpp)
- Medium 80题 Remove Duplicates from Sorted Array II
- LeetCode-Array-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
- 传感器获取旋转矩阵R,机身坐标系和世界坐标系的映射remapR,获得旋转角度 orientationValues;
- Eclipse使用总结
- sgu297:Fair-play(水题)
- 设计模式_状态模式
- HDU 1222 Wolf and Rabbit 幸存的兔子
- [leetcode 80, Medium] Remove Duplicates from Sorted Array II
- 关于C/C++中内存分配与释放的疑惑?
- crossApp初级-CAIamgeView-6
- poj 2031 Building a Space Station
- 黑马程序员——Java基础-IO流
- 服务器的响应数据
- MySQL-5.6.25数据库免安装配置
- 熄灯问题(百练2811)
- JavaWeb学习笔记:第一个 WEB 应用程序