LeetCode80——Remove Duplicates from Sorted Array II
来源:互联网 发布:扶贫软件 编辑:程序博客网 时间:2024/05/29 18:03
LeetCode80——Remove Duplicates from Sorted Array II
题意:
让非递减的有序数组中重复的数字不超过2个。
思路:
两个索引i和j
i是大循环
j是每组相同数字内部为了统计遍历
就是从头到尾遍历并计数,因为是有序的,计数都是连续的比较方便。
一旦计数变量count超过2,则需要进行以下操作:
1.删去该元素
2.j-- ( 删去元素的索引的前一个元素)
当找完一组相同数据之后:
1.i=j-1 让i跳过重复的元素,从下一组重复元素的首元素开始。
代码:
class Solution {public:int removeDuplicates(vector<int>& nums) {int count = 0;int sum=0;for (int i = 0; i < nums.size(); i++){int temp = nums[i];int j = i;while ( nums[j] == temp)//一致{count++;if (count > 2){nums.erase(nums.begin() + j);//删除重复元素j--;//索引减1,表示回退到删除元素之前的位置count = 2;}j++;if (j > nums.size()-1)break;}sum += count;count = 0;i = j-1;//跳过重复元素}return sum;}};
0 0
- LeetCode80——Remove Duplicates from Sorted Array II
- leetcode80 Remove Duplicates from Sorted Array II
- leetcode80. Remove Duplicates from Sorted Array II
- leetcode80 Remove Duplicates from Sorted Array II
- LeetCode80. Remove Duplicates from Sorted Array II
- leetcode80~Remove Duplicates from Sorted Array II
- leetcode80. Remove Duplicates from Sorted Array II
- LeetCode80 Remove Duplicates from Sorted Array II
- leetcode80[medium]--Remove Duplicates from Sorted Array II
- [LeetCode80]Remove Duplicates from Sorted List II
- leetcode——26. Remove Duplicates from Sorted Array && 80. Remove Duplicates from Sorted Array II
- leetcode 080 —— Remove Duplicates from Sorted Array II
- 数组——remove-duplicates-from-sorted-array-ii
- Leetcode——80. Remove Duplicates from Sorted Array II
- 算法系列——Remove Duplicates from Sorted Array II
- LeetCode—Remove Duplicates from Sorted Array II
- Algorithms—80.Remove Duplicates from Sorted Array II
- LeetCode—Remove Duplicates from Sorted Array
- Webkit之QStyle样式
- Doxygen + Graphviz windows+Windows Help Workshop下安装配置(图解)
- 无权最短路径
- 书上说Java中方法要用类名调用,我发现
- 带你飞系列-二搜索进阶
- LeetCode80——Remove Duplicates from Sorted Array II
- 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法
- WIN2003下休眠后无线连接无法连接
- Java之路--2015年11月17日
- poj Parallelogram Counting 1971 (哈希表) 好题
- 软件工程第五章作业
- WebKit之目录结构简介
- 15-11-17 标识符
- Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么?