题目:

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 nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. (Easy)

分析:

这道题和下一个都是简单的双重指针倒腾数组元素的题。

开始做的时候居然一直想swap,导致代码有点复杂,而且有一些情况开始没想到。

当时去摩根面试的时候第一题就是这个,想想自己当时真是水的不行,怪不得被刷了...

代码1:(swap的,要多一个cur记录是否重复,因为交换后会出现没法跟前一个比较。 不知道为什么脑子秀逗一直要swap...)

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == ) {
return ;
}
int p1 = , p2 = ;
int cur = nums[];
while (p1 != nums.size()) {
if (nums[p1] != cur) {
cur = nums[p1];
if (p1 != p2) {
swap(nums[p1], nums[p2]);
}
p2++;
}
p1++;
}
return p2;
}
};

代码2: (不要交换,就是两个指针,有一个维护所有不重复的元素,发现就拷过去,这多简单....)

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == ) {
return ;
}
int p1 = , p2 = ;
while (p1 != nums.size()) {
if (nums[p1] != nums[p1 - ]) {
nums[p2] = nums[p1];
p2++;
}
p1++;
}
return p2;
}
};

代码3: (写成for循环虽然双重指针没那么明显,但是代码好看一些,以后还是这么写)

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == ) {
return ;
}
int p = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] != nums[i - ]) {
nums[p] = nums[i];
p++;
}
}
return p;
}
};
 

LeetCode26 Remove Duplicates from Sorted Array的更多相关文章

  1. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

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

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

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

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

  4. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  5. 【leetcode】Remove Duplicates from Sorted Array II

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

  6. 26. Remove Duplicates from Sorted Array

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

  7. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

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

  8. LeetCode:Remove Duplicates from Sorted Array I II

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

  9. 【26】Remove Duplicates from Sorted Array

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

随机推荐

  1. dom cookie记录用户名

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. dom 输入文字模拟滚动

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. devexpress皮肤设置

    DEV的皮肤管理控件:SkinController: TdxSkinController; 皮肤设置:SkinController.SkinName := appInfo.SkinName; TdxR ...

  4. http://docwiki.embarcadero.com/RADStudio/XE7/en/Delphi_Data_Types

    http://docwiki.embarcadero.com/RADStudio/XE7/en/Delphi_Data_Types

  5. opencv win7 配置

    按照官网的配置方法,结果出现了下面的错误信息F:\eclipse C++ workspace\test\Debug/../src/test.cpp:18: undefined reference to ...

  6. hdu 3397 Sequence operation(很有意思的线段树题)

    Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. Unity3d:使用uWebKit插件嵌入网页,网页中的flv视频无法播放

    问题描述:unity3d程序,使用uWebKit插件嵌入网页,用来播放FLV视频,有的电脑可以正常播放,有的电脑在网页中播放不了ps:网页中的播放器用的是player.swf解决方案:是由于网页中的播 ...

  8. Proxy代理(AOP实现原理)

    AOP基于动态代理实现:返回代理对象 java.lang.reflect 包Proxy 类,构造代理类.newProxyInstance() 就是创建代理对象的方法. Proxy.newProxyIn ...

  9. WCF 客户端调用几种方式

    http://www..com/html/blogs/20130413/2116.htm CSDN123 http://developer.51cto.com/art/200911/161465.ht ...

  10. C#判断一个类中有无"指定名称"的方法

    C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...