[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. ie8下new Date()指定时间

    项目开发中很可能会需要创建一个指定日期的时间对象,火狐浏览器可以直接使用new Date('yyyy-mm-dd')生成时间,但是在ie8下就会发现生成的时间输出的是NaN-NaN-NaN.为了兼容可 ...

  2. Spark大型电商项目实战-及其改良(4) 单独运行程序发现的问题

    之前的运行结果比对发现,有1个函数的作用在2个job里面是相同的,但是对应的计算时间却差太远 于是把4个job分开运行.虽说使用的数据不同,但是生成数据的生成器是相同的,数据排布差距不大,数据量也是相 ...

  3. HAproxy指南之haproxy实现动静分离(案例篇)

    HAproxy指南之haproxy实现动静分离(案例篇) 转自   https://blog.51cto.com/blief/1751806   实际应用环境中,往往需要根据业务请求将相关不同请求跳转 ...

  4. Docker 运维高级应用管理

     Docker 基本应用 1.Docker 介绍及安装 2.Docket 使用命令 3.Docker run命令参数整理 4.Docker 构建镜像 Docker Compose 高级应用 1.Doc ...

  5. 常用sql语句总结(一)(查询)

    常用sql语句总结(一)(查询) 数据操作语句:DML 数据定义语句:DDL 数据控制语句:DCL (执行顺序------序号) 一.基本查询: 1. SELECT * ----- 2 FROM 数据 ...

  6. 2018年山东省省队集训 Round 1 Day 2简要题解

    从这里开始 Problem A 生日礼物 Problem B 咕咕 Problem C 解决npc (相信来看这篇博客的人都有题面) T2以为可以线性递推,然后花了两个小时.然后想了两个小时T1,会了 ...

  7. restorecon【转】

    本文转载自:https://blog.csdn.net/sinat_36888624/article/details/6076650 estorecon命令用来恢复SELinux文件属性即恢复文件的安 ...

  8. 动态生成具有嵌套属性的linq选择(select)

    class SelectItem { public string Item { get; set; } } class SelectList { public int ID { get; set; } ...

  9. 剑指offer 08:跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). public class Solution { public int ...

  10. DLL 破解工具

    1.Reflecter+Reflexil (第一类) 2.本文使用的工具下载地址为:(第二类) https://github.com/cnxy/dnSpy/archive/v4.0.0.zip 或 d ...