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. spring 管理 jdbc 事务

    @Transactional    业务实现类 类名上方--这个类中的方法,执行操作前会打开事务. 默认:RuntimeException 自动回滚, 可以try  catch 的异常,不会滚 方法名 ...

  2. tomcat部署

    因为之前一直用的JBoss服务器,今天测试了一下想换成tomcat,然后就考了一个现成的tomcat,然后将jboss下的项目复制了一个到tomcat下,然后改了一下文件名,结果启动时报错 java. ...

  3. 优秀的PHP开源项目集合

    包管理Package Management Package Management Related 框架 框架组件 微框架Micro Frameworks 内容管理系统Content Managemen ...

  4. 【PHP】$_POST, $HTTP_RAW_POST_DATA, and php://input

    1.HTML <form> enctype Attribute application/x-www-form-urlencoded  传送之前所有的字符都会被encoded,(spaces ...

  5. 【mysql】一个关于order by排序的问题

    I have a table CREATE TABLE `tableMain` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value1` varchar(45) ...

  6. cocos2d-x之Vector与map

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  7. .net framework 4.6.2 下载

    .net framework   .net framework版本: 4.6.2 File Name: NDP462-KB3151800-x86-x64-AllOS-ENU.exe 发布日期: 201 ...

  8. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  9. Linux Commands intro1

    $((expression)) echo $(2+2) :wrong echo $((2+2))  : right echo Front-{A,B,C}-Back Front-A-Back Front ...

  10. (转)github设置添加SSH

    很多朋友在用github管理项目的时候,都是直接使用https url克隆到本地,当然也有有些人使用 SSH url 克隆到本地.然而,为什么绝大多数人会使用https url克隆呢? 这是因为,使用 ...