[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. python学习之爬虫初体验

    作业来源: "https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2851" ** 1.简述爬虫原理 通用爬虫 即(搜索 ...

  2. v-charts简介

    一, v-charts简介 在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出现正是为了解决这个痛点.基于 Vue2.0 和 echarts 封 ...

  3. Linux代理服务器—squid正向代理实验

    1.代理服务器squid简介 Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器.Squid有广泛的用途,从作为网页服务器的前置cac ...

  4. iOS项目之使用开关控制日志输出的功能

    最近一直在做sdk的项目,用户提出了一个需求,需要屏蔽sdk内部的日志输出.由于sdk内部的日志是为了调试,如果屏蔽了肯定不方便,所以研究了一下日志输出开关的功能. 在这里介绍两种实现方案:一种方案是 ...

  5. hiho一下 第207周

    题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...

  6. flutter 读取sdcard权限问题相关

    https://stackoverflow.com/questions/46698751/permission-denied-at-externalstoragedirectory-access-vi ...

  7. Pycharm 的设置--参数设置(运行.py文件带参数,例如argument)

    程序运行时,如果需要输入参数,如下图中程序代码: 在生成exe后,程序在控制台下运行时格式为: 这种情况在调试程序时,如果只是在Pycharm环境中简单执行“Run”菜单下的“Run”命令,会出现以下 ...

  8. Iview的小经验

    1.动态控制form验证的小红星 HTML部分: <FormItem :class="{requireStar:bankFlag1}"> CSS部分: /*动态必填项c ...

  9. go-switch特点

    程序中遇到有枚举分支逻辑时,需要用到 switch 代替多个 if else 判断. 学习过程遇到一些与C#不同点,记录下. 语法: switch expr { case x1 : //expr为x1 ...

  10. robotframework-ride支持python3

    最近发现robotframework的RIDE工具终于支持python3了,赶紧就安装了一下. 最新版本1.7.3.1基于wxPython4.0.4,此时的wxPython也是支持Python3.x的 ...