题目:

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. js 多物体运动

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

  2. 各种边框样式。。本以为border不是这么用的。

    关于文本框样式□ 文本框样式 □ 显示虚线边框的文本框(IE5.5才可看到效果)  <INPUT style="border-width: 1px,1px,1px,1px;border ...

  3. js中Number数字数值运算后值不对

    问题: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这样,两 ...

  4. java进程状态

    A thread state. A thread can be in one of the following states: NEWA thread that has not yet started ...

  5. C# 抽象类和接口的区别

    从表象上来说,抽象类可以给出一些成员的实现,而接口却不包含成员的实现,抽象类的成员可以被继承类来部分实现,而接口类中的成员要子类来全部实现 .还有一个类可以实现多个接口,但只可以继承一个抽象类,这只是 ...

  6. 如何为C语言添加一个对象系统

    为C语言添加OO能力的尝试从上世纪70年代到现在一直没有停止过,除了大获成的C++/Objective-C以外,还有很多其它的成功案例,比如GTK在libg中实现了一个对象系统,还有前几年一个OOC, ...

  7. VTK序列图像的读取[转][改]

    医学图像处理的应用程序中,经常会碰到读取一个序列图像的操作.比如CT.MR等所成的图像都是一个切面一个切面地存储的,医学图像处理程序要处理这些数据,第一步当然是把这些数据从磁盘等外部存储介质中导入内存 ...

  8. STL中vector容器实现反转(reverse)

    vector容器中实现可以通过以下两种方式实现: #include "stdafx.h" #include <vector> #include <iostream ...

  9. java正则表达式语法详解及其使用代码实例

    原文地址 译者序(下载代码) 正则表达式善于处理文本,对匹配.搜索和替换等操作都有意想不到的作用.正因如此,正则表达式现在是作为程序员七种基本技能之一*,因此学习和使用它在工作中都能达到很高的效率. ...

  10. AIM Tech Round (Div. 2) A. Save Luke 水题

    A. Save Luke 题目连接: http://codeforces.com/contest/624/problem/A Description Luke Skywalker got locked ...