题目:

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. C 基于socket实现简单的文件传输

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAicAAAA5CAIAAABicRxIAAAgAElEQVR4nOy9Z5NVV5om+rzL773POW

  2. 两年内从零到每月十亿 PV 的发展来谈 Pinterest 的架构设计(转)

    原文:Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years 译文:两年内从零到每月十亿 PV ...

  3. 使用bat快速创建cocos2d-x模板

    在上一篇文章中我们学习了如何使用python创建cocos2d-x 2.2工程,但是每次我们都输入一大串的命令,好烦好烦啊.参考别人的文章这里写了一个bat,如下 @echo off echo --- ...

  4. JXTA中定义自己的成员服务

    http://blog.csdn.net/neusoftware_20063500/article/details/4302903 —————————————————————————————————— ...

  5. 网易及新浪IP查询接口

    通过IP地址获取对应的地区信息通常有两种方法:1)自己写程序,解析IP对应的地区信息,需要数据库.2)根据第三方提供的API查询获取地区信息. 第一种方法,参见文本<通过纯真IP数据库获取IP地 ...

  6. UVALive 7079 - How Many Maos Does the Guanxi Worth(最短路Floyd)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  8. Oracle的体系结构

    前言 这个章节主要想说的是Oracle的体系结构,这个也是理论强些.还有一些比较理论的知识点(比如表空间啊),就暂时先不写了,下一章节开始进入Oracle的操作阶段,比如表的查询啊.插入以及重点是和S ...

  9. Swift 实现Bitmask Option(Enum)

    在Swift中实现ObjC中的NS_OPTION不是通过enum,而是通过conform RawOptionSetType protocol的struct来实现的. 代码如下: struct Test ...

  10. 【M34】如何在同一个程序中结合C++和C

    1.C++和C混合编程的时候,需要考虑产生的目标文件的兼容性. 2.名称重整,为什么要搞出名称重整? 连接器要求所有方法名必须独一无二.对于C语言,没问题.C++支持过载,也就是方法名相同,形参表不同 ...