【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 ...
随机推荐
- iOS 修改UIWebView的UserAgent
iOS和H5交互的时候,H5需要用userAgent带一些参数,需要我们修改默认的UserAgent为自定义的. 首先,给大家普及一下userAgent的历史,点击UserAgent查看. 1 在Ap ...
- Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析
经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...
- php写入txt换行符
1.问题 写入txt文件想换行,老是直接输出了\r\n. 2.解决 要用双引号对\r\n进行解释,否则php会直接当字符输出. 3.例子 要求:往test.txt文本每一行后面加abc $a=file ...
- Unable to locate player settings. bin/Data/settings.xml
Hello guys, so according to the this response: http://stackoverflow.com/a/18302624/5727136 you need ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- 32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络
32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络 32 GroupSock(AddressPortLookupTable)——live555 ...
- Tomcat启动服务报错:Unknown version string [3.1]. Default version will be used.
用Intellij IDEA 部署Web项目,Tomcat启动后报错Unknown version string [3.1]. Default version will be used. 作者的问题出 ...
- 【GoLang】转载:我为什么放弃Go语言,哈哈
我为什么放弃Go语言 作者:庄晓立(Liigo) 日期:2014年3月 原创链接:http://blog.csdn.NET/liigo/article/details/23699459 转载请注明出处 ...
- C#函数参数
当函数接受参数时,必须指定下属内容 函数在其定义中指定参数列表,以及这些参数的类型 在每个函数调用中匹配参数列表 参数匹配:当调用函数时,必须使参数与函数定义中指定的参数完全匹配,这意味着要匹配参数的 ...
- 转: UAC 问题
打开VS2005.VS2008.VS2010工程,查看工程文件夹中的Properties文件夹下是否有app.manifest这个文件:如 没有,按如下方式创建:鼠标右击工程在菜单中选择“属性”,点击 ...