【leetcode刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
题解:
设置两个变量:右边kepler和前向游标forward。如果当前kepeler所指的元素和它下一个元素相等,那么向A中存入这两个相等的元素,然后将forward置为kepeler+2,向前移动,如果碰到的元素都跟当前kepeler所指的元素相等,就忽略,否则,将kepeler移向forward所指元素,继续循环;如果当前kepeler所指的元素和它下一个元素不相等,那么就把这个元素直接加入A中,kepeler前移,继续循环。
例如题目中给的数组:1,1,1,2,2,3.
初始kepeler指向第一个1,发现kepeler+1也是一个1,就把forward置为kepeler+2=2,向前探测。探测到index为3的元素时候,发现和kepeler所指的元素不相等,就把kepeler移到索引为3处,继续上述过程,最终得到数组1,1,2,2,3.
代码如下:
public class Solution {
public int removeDuplicates(int[] A) {
if(A == null || A.length == 0)
return 0;
int kepeler = 0,forward = 0;
int NewSize = 0;
while(kepeler < A.length){
if(kepeler+1 < A.length && A[kepeler+1] == A[kepeler]){
A[NewSize++] = A[kepeler]; //相同的元素有两个,都放入数组中
A[NewSize++] = A[kepeler];
forward = kepeler+2;
while(forward < A.length && A[forward] == A[kepeler])
forward++;
kepeler = forward;
}
else if(kepeler < A.length){
A[NewSize++] = A[kepeler];
kepeler++;
}
}
return NewSize;
}
}
【leetcode刷题笔记】Remove Duplicates from Sorted Array II的更多相关文章
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
随机推荐
- 打造自己的LINQ Provider(上):Expression Tree揭秘
概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...
- poj1408(求线段交点)
求出所有线段的交点,然后利用叉乘求四边形面积即可. // // main.cpp // poj1408 // // Created by 陈加寿 on 15/12/31. // Copyright ( ...
- 九度OJ 1323:World Cup Betting(世界杯) (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:492 解决:219 题目描述: With the 2010 FIFA World Cup running, football fans th ...
- The space of such functions is known as a reproducing kernel Hilbert space.
Reproducing kernel Hilbert space Mapping the points to a higher dimensional feature space http://www ...
- 被学长教会的高斯消元法Gauss
昨天学长教了我高斯消元法. 这里用一个栗子来模拟一下Gauss的流程. 真的通俗易懂!这里是洛谷题目链接. 这就是例子 x-2y+3z= 4x-5y+6z= 7x-8y+10z= 先将它转化为矩阵 - ...
- activiti基础--2----------------------(流程定义)
Deployment 部署对象 1.一次部署的多个文件信息,对于不需要的流程可以删除和修改 2.对应的表 act_re_deployment #部署对象表 act_re_procdef #流程定义表 ...
- linux 10 -Bash Shell编程
二十三. Bash Shell编程: 1. 读取用户变量: read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入.在read命令后 ...
- C#访问数据库的步骤
1.必须导入包含适当的ADO.NET类的名称空间 2.获取具体的数据库连接字符串. 3.实例化Connection对象,并建立.打开连接. 4.使用Command对象,从数据库存取器中读取数据和向数据 ...
- 使用svn diff的-r参数的来比较任意两个版本的差异
1 svn diff的用法1.1 对比当前本地的工作拷贝文件(working copy)和缓存在.svn下的版本库文件的区别 svn diff 1.2 对比当前本地的工作拷贝文件(working co ...
- linux基础part2
linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...