【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
思路:
我用两个游标分别记录新数组的下一个位置newpos和原数组要判断的下一个位置pos。如果pos位置的数字和newpos - 1 的一样则跳过
关键:比较是否一样时与新的数组的目前最后一个数字比。
int removeDuplicates(int A[], int n) {
if(n == ) return ;
int newpos = , pos = ;
while(pos < n)
{
if(A[pos] == A[newpos - ]) pos++;
else A[newpos++] = A[pos++];
}
return newpos;
}
大神超短的代码:
int count = ;
for(int i = ; i < n; i++){
if(A[i] == A[i-]) count++;
else A[i-count] = A[i];
}
return n-count;
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].
允许两个重复的
思路:与上面其实都一样,就是把判断条件改为与上两个数字相同就跳过。注意,要与新数组最后两个数字比较。
int removeDuplicatesTwice(int A[], int n) {
if(n <= ) return n;
int pos = ; //新数组的下一个判断位置
for(int i = ; i < n; i++)
{
if(!(A[i] == A[pos - ] && A[i] == A[pos - ])) //这里是关键,要和pos-1 和 pos-2比较。pos对应的出现两次才是真的两次了。不能和i-1和i-2比,因为可能在前面这两个位置的数字已经改变了。
{
A[pos++] = A[i];
}
}
return pos;
}
有人写了一个问题泛化到可以重复k次的代码:
int removeDuplicates(int A[], int n, int k) {
if (n <= k) return n;
int i = , j = ;
int cnt = ;
while (j < n) {
if (A[j] != A[j-]) { //其实我觉得这里写成A[j] != A[i - 1] 更好
cnt = ;
A[i++] = A[j];
}
else {
if (cnt < k) {
A[i++] = A[j];
cnt++;
}
}
++j;
}
return i;
}
【leetcode】Remove Duplicates from Sorted Array I & II(middle)的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【Leetcode】【Easy】Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【数组】Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)
1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...
- 【leetcode】Remove Duplicates from Sorted List
题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
随机推荐
- Slave_SQL_Running: No mysql同步故障解决方法
Slave_SQL_Running: No mysql同步故障解决 今天检查数据库发现一台MySQL Slave未和主机同步,查看Slave状态:mysql> show slave s ...
- jQuery属性选择器.attr()和.prop()两种方法
在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...
- QT连接Linux mysql注意
windows: #define MYSQLDB "QMYSQL"#define MYSQLDB_HOSTNAME "192.168.228.168"#defi ...
- PHP基础之 重载 的实现方式
===================PHP中的伪重载Overloading================== PHP中没有像C#或java中的重载,但可以通其它方法实现重载 重载:属性重载与方法重 ...
- informatica通用命令方式启动workflow
#传mapping参数时#调用执行workflow,-sv informatica服务,-d 域 -u 用户,-p 密码 -folder知识库下的workflow所在文件夹,-wait wf_test ...
- mongodb university week4
1.index Creation,background 如果在foreground运行index,会阻塞其他writer,如果background运行,会比较慢,但不会阻塞其他writer,可以并发写 ...
- All Kind Of Conference(随时更新...)
收集一些前端开发的各种会议,里面有视频或者PPT,随时查看都还是很有收获的.还有要向这些演讲的前辈看齐- AC 2015:http://ac.alloyteam.com/2015/ AC 2016:h ...
- OpenGL中平移、旋转、缩放矩阵堆栈操作
在OpenGL中,图元的几何变换均为线性变换,通过矩阵变换实现.OpenGL中的坐标用齐次坐标表示,即(x,y,z)表示成(x',y',z',h),其中x=x'/h; y=y'/h; z=z'/h. ...
- caffe学习系列(6):其他层介绍
主要包括softmax-loss层(与softmax有区别),全连接层(Inner Prouduct),accuracy层,reshape层, Dropout层. softmax: layers { ...
- TP框架自动加载优先级
$map = array('Think\Log'=>THINK_PATH.'Think\Log.php','Org\Util\Array'=>THINK_PATH.'Org\Util\Ar ...