LeetCode:Remove Duplicates from Sorted Array

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].

分析:从数组的第二个元素开始遍历,把和上一个位置的值不同的的元素保存下来,注意一下n == 0的情形(这里我们直接是inplace操作)

class Solution {
public:
int removeDuplicates(int arr[], int n) {
if (n == ) {arr = NULL;return ;}
int index = ;
for(int i = ; i < n; i++)
if(arr[i] != arr[i-])
arr[index++] = arr[i];
return index;
}
};

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].

分析:和上一题相似,从数组的第三个元素开始遍历,把和前一个的前一个(A[i-2])的值不同的元素保存下来,这里需要注意的是原来的A[i-2]的值可能已经被覆盖了,比如1,1,1,2,2,3遍历到第4个元素2时,需要保存,并且保存在了第三个位置数组变成了1,1,2,2,2,3,遍历到第5个元素2时,前一个的前一个元素本来是1,但是前面已经被2给覆盖了,所有我们需要保存上一次被覆盖元素的索引和值。                                       本文地址

class Solution {
public:
int removeDuplicates(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//只要当前元素和前一个的前一个不同就保留
if(n == ) {A = NULL;return ;}
if(n <= )return n;
int index = ;
int lastChange, lastChangeIndex = -; //上一次被覆盖的位置和被覆盖的值
for(int i = ; i < n; i++)
{
int prepre = A[i-];
if(i- == lastChangeIndex)prepre = lastChange;//原来的A[i-2]有可能被覆盖了
if(A[i] != prepre)
{
lastChangeIndex = index;
lastChange = A[index];
A[index++] = A[i];
}
}
return index;
}
};

还有一种更优雅的方法是,当我们需要留下某个元素时,先暂时保存好,等到下一轮再覆盖,这样我们找前一个的前一个元素A[i-2]时,就不会出现A[i-2]原来的值被覆盖的情况。

class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n==)return ;
if(n==)return ;
int num=,i,temp=A[];
for(i=;i<n;++i)
if(A[i]!=A[i-])
{
A[num++]=temp;
temp=A[i];
}
A[num++]=temp;
return num;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3461401.html

LeetCode:Remove Duplicates from Sorted Array I II的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  2. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  3. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  4. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  5. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  6. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  7. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  8. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  9. 【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 ret ...

随机推荐

  1. windows 7 下,如何统计某文件夹下 视频总时长

    由于项目需要,我需要给系统加权限,这真是一个让人头疼的问题,如果要每个业务方法都加上权限判断逻辑,那真的不敢想象是多么大的工作量,日后有变动的话,我会不会发疯? 所以我必须利用之前学到的AOP编程,在 ...

  2. openswitch db files

    http://openvswitch.org/support/dist-docs/ovsdb-tool.1.html FILES The default db is /etc/openvswitch/ ...

  3. Effective Java 45 Minimize the scope of local variables

    Principle The most powerful technique for minimizing the scope of a local variable is to declare it ...

  4. Windows Server 2008 下ASP程序连接ORACLE数据库驱动错误

    今天开发那边升级.改造系统过程中,在测试服务器碰到关于ASP程序连接ORACLE数据库的小问题,虽然是小问题,但是整起来真要命啊,花了不少时间,主要是ASP程序啊,这种上古神器,哥还是当年毕业的时候弄 ...

  5. MongoDB学习笔记——数据库操作

    使用use数据库名称来创建数据库,如果该数据库已经存在则返回这个数据库 语句格式:use DATABASE_NAME >use mynewdb switched to db mynewdb 使用 ...

  6. Uploadify文件上传

    一.简介 Uploadify 是一种基于html5 或 flash的多文件上传的jQuery插件.Uploadify可以支持多种定制.它是一种异步的文件上传插件.下载网站为http://www.upl ...

  7. 烂泥:【解决】ubuntu提示ilanni不在sudoers文件中错误

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天在Ubuntu系统中新建一个用户ilanni,添加完毕后.在执行有关sudo的命令时,提示如下的错误信息:ilanni 不在 sudoers 文件中 ...

  8. Linux iptables

    一.简介 http://liaoph.com/iptables/ 二.操作 1)查看规则 iptables -t filter -L -n iptables -t nat -L -n iptables ...

  9. 23 其它话题 - 《Python 核心编程》

  10. 如何用js实现截取一个字符串中的数字

    比如var v ="我要提问1098";var v="我0要提问"var v="我还是要提问987"等我想要里边的 1098 ,0, 987 ...