专栏首页leetcode_solutions26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array

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.

【实现】

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length < 1) return 0;
        int n = nums.length;
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (nums[j] != nums[i]) {
                nums[++j] = nums[i];
            }
        }
        return j + 1;
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Leetcode 26. Remove Duplicates from Sorted Array

    版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn....

    Tyan
  • Leetcode 26 Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element app...

    triplebee
  • leetcode: 26. Remove Duplicates from Sorted Array

    leetcode: 80. Remove Duplicates from Sorted Array II

    JNingWei
  • LeetCode 26 Remove Duplicates from Sorted Array

    一份执着✘
  • leetcode 26 Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted ArrayTotal Accepted: 66627 Total Submissions: 2127...

    流川疯
  • leetcode 26 Remove Duplicates from Sorted Array

    @坤的
  • 【Leet Code】26. Remove Duplicates from Sorted Array

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    韩旭051
  • LeetCode笔记:26. Remove Duplicates from Sorted Array

    这道题和LeetCode笔记:27. Remove Element这个很类似,只不过这道题的难度在于,在操作中不能随意改变数组中原本元素的位置,因为你要保持它后...

    Cloudox
  • 【LeetCode每日一题】26. Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element...

    公众号-不为谁写的歌
  • 关小刷刷题08 – Leetcode 26. Remove Duplicates from Sorted Array 方法2、3

    关小刷刷题08 – Leetcode 26. Remove Duplicates from Sorted Array 方法2、3 方法2 方法2:遍历数组,遇到...

    WZEARW
  • 关关的刷题日记07——Leetcode 26. Remove Duplicates from Sorted Array 方法1

    关小刷刷题07 – Leetcode 26. Remove Duplicates from Sorted Array 方法1 题目 Given a sorted...

    WZEARW
  • LeetCode-26.Remove Duplicates from Sorted Array | 删除排序数组中的重复项

    Given a sorted array nums, remove the duplicates in-place such that each element...

    Zoctopus
  • Remove Duplicates from Sorted Array

    问题:将有序的数组中重复的数字去掉 分析:由于有序所以只用和前一个比较就行 class Solution { public: int removeDup...

    用户1624346
  • Remove Duplicates from Sorted Array

    青木
  • Remove Duplicates from Sorted Array II

    Tyan
  • Q26 Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in-place such that each element appe...

    echobingo
  • Remove Duplicates from Sorted Array II

    问题:消除数组中重复次数超过三次的多余的数 分析:若ai-1==ai-2若ai也相等,则清楚ai class Solution { public: in...

    用户1624346
  • Remove Duplicates from Sorted Array II

    青木
  • Leetcode: Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each eleme...

    卡尔曼和玻尔兹曼谁曼

扫码关注云+社区

领取腾讯云代金券