Remove Duplicates from Sorted Array II问题及解法
来源:互联网 发布:随机生成域名检测 编辑:程序博客网 时间:2024/05/16 15:56
问题描述:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
示例:
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 j = 0;int s = 0;for (int i = 0; i < nums.size(); i++){if (nums[i] == nums[s]){if (i - s < 2){nums[j++] = nums[i];}}else{s = i;nums[j++] = nums[i];}}return j; }};
阅读全文
0 0
- Remove Duplicates from Sorted Array II问题及解法
- Remove Duplicates from Sorted Array问题及解法
- Remove Duplicates from Sorted List II问题及解法
- leetcode:Remove Duplicates from Sorted Array II 菜鸟解法
- 【LeetCode】80. Remove Duplicates from Sorted Array II解法及注释
- Leetcode之Remove Duplicates from Sorted Array II 问题
- Remove Duplicates from Sorted List问题及解法
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- 1172: 矩阵边界和(指针专题)
- 函数的声明方式
- C++关联容器类:集合和多重集合类、映射和多重映射类
- js之数组
- 根据url生成二维码
- Remove Duplicates from Sorted Array II问题及解法
- 2017.8.2——广搜(bfs)+队列的使用
- 并查集
- poj1845 逆元 因子和
- Linux命令集
- 牛顿法求解立方根
- 实现一个线程安全的单例模式
- PHP基础教程-09 浮点型变量
- Zeppelin源码分析-调度和资源分析(1)