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

描述

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

Given 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 returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度。 
不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点。 
示例:

给定数组: nums = [1,1,2],
你的函数应该返回新长度 2, 并且原数组nums的前两个元素必须是1和2
不需要理会新的数组长度后面的元素

解析

我们使用快慢指针来记录遍历的坐标,最开始时两个指针都指向第一个数字,如果两个指针指的数字相同,则快指针向前走一步,如果不同,则两个指针都向前走一步,这样当快指针走完整个数组后,慢指针当前的坐标加1就是数组中不同数字的个数。

代码

class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length <= 0) {
return 0;
}
if (nums.length < 2) {
return 1;
}
int pre = 0, cur = 0, n = nums.length;
while (cur < n) {
if (nums[pre] == nums[cur])
++cur;
else
nums[++pre] = nums[cur++];
}
return pre + 1;
}
}

我们也可以用for循环来写,这里的j就是上面解法中的pre,i就是cur,所以本质上都是一样的,参见代码如下:

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int j = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
if (nums[i] != nums[j]) nums[++j] = nums[i];
}
return j + 1;
}
};

[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)的更多相关文章

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  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] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  5. 026 Remove Duplicates from Sorted Array 从排序数组中删除重复项

    给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度.不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点.示例:给定数组: nums ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  7. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

  9. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

随机推荐

  1. virtualenv的使用

    virtualenv 是一个创建隔绝的Python环境的工具.在向服务器导入第三方库的时候特别有用. 1.首先,安装virtualenv pip install virtualenv 2.创建一个虚拟 ...

  2. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165215

    2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165215 目录 Kali的下载及安装 网络配置 设置共享文件夹及剪切板 更新软件源 [Kali的下载及安 ...

  3. javascript实现继承的三种方式

    一.原型链继承  function Parent(){} function Child(){} Child.prototype = new Parent(); 通过对象child的prototype属 ...

  4. PyGame实现情人节表白利器

    前提:写不出那么那个的话哇,随便写写,随便看看,重在代码(文章末尾有免费完整源代码) 实验环境: pygame 1.9.4 pycharm python3.6 实现思路: pygame.display ...

  5. 关于 diff 和patch

    参考: https://blog.csdn.net/zygblock/article/details/53384862 diff和patch是 版本控制 git 的不可缺少的工具 diff 是用来比较 ...

  6. Summary on deep learning framework --- TensorFlow

     Summary on deep learning framework --- TensorFlow Updated on 2018-07-22 21:28:11 1. Check failed: s ...

  7. IIS简单的反向代理设置

    下载IIS扩展 1.URL Rewrite 地址: https://www.iis.net/downloads/microsoft/url-rewrite 2.Application Request ...

  8. Python3学习笔记--迭代器

    迭代 使用一个循环来遍历某个东西时这个过程本身叫做迭代. 可迭代对象 python中只要定义了可以返回一个迭代器的__iter__方法,或者定义了可以支持下标索引的__getitem__方法,那么它就 ...

  9. eclipse报错:Multiple annotations found at this line: - String cannot be resolved to a type解决方法实测

    Multiple annotations found at this line:- String cannot be resolved to a type- The method getContext ...

  10. kafka可视化客户端工具Kafka Tool

    1.下载 下载地址:http://www.kafkatool.com/download.html 2.安装 根据不同的系统下载对应的版本,我这里kafka版本是1.1.0,下载kafka tool 2 ...