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)的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  3. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  4. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  5. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  6. 【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 ...

  7. 【数组】Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  8. 【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 ...

  9. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

随机推荐

  1. NHibernate配置

    因为NHibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了. NHibernate.Test.dll包含了一个示例的配置文件app.config, ...

  2. 修改dedecms默认文章来源 "未知"改为关键词

    在dedecms后台发表文章时文章来源是可选的,有时我们没有选择或没填写,那么前台默认文章来源即“未知”.如何将dedecms默认文章来源改为自己想要的关键词呢?即将“未知”改为“keyword”呢? ...

  3. hadoop之 flume1.6安装

    flume 1.6安装1.解压 2.复制 cp conf/flume-conf.properties.template conf/flume.conf cp conf/flume-env.sh.tem ...

  4. linux下svn命令使用大全

    最近经常使用svn进行代码管理,这些命令老是记不住,得经常上网查,终于找了一个linux下svn命令使用大全:1.将文件checkout到本地目录 svn checkout path(path是服务器 ...

  5. C#之XMAL与WPF

    XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Butt ...

  6. C#入门随手笔记

    1..Net Framework是Microsoft为开发应用程序而创建的一个开发平台. 运行操作系统不限:Microsoft版本运行在windows,Mono版本运行开Linux和MacOS: 应用 ...

  7. Swift语法入门

    正文参考: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Progra ...

  8. 如何打开xip格式的xcode安装包

    解决方法如下: 1.保证存储空间 20G 2.去除解压验证 xattr -d com.apple.quarantine Xcode_8_beta.xip 3.双击解压 详见: 从官网下载的 xcode ...

  9. json不转化值是null的字段

    今天写东西,发现JSONObject.fromObject(),方法,会把value是null的字段,转为0或"",就自己写了一个方法,如果value是null就不转换 packa ...

  10. Android学习笔记(九)——布局和控件的自定义

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件 ...