前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Leet Code】26. Remove Duplicates from Sorted Array

【Leet Code】26. Remove Duplicates from Sorted Array

作者头像
韩旭051
发布2019-11-08 11:54:31
3260
发布2019-11-08 11:54:31
举报
文章被收录于专栏:刷题笔记

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

本文链接:https://cloud.tencent.com/developer/article/1535447

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++) {

代码语言:txt
复制
 print(nums[i]);

}

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

辣鸡代码

代码语言:javascript
复制
int removeDuplicates(int* nums, int numsSize){
    int p1=0,p2=0;
    for(int p2=1;p2<numsSize;p2++){
        if(nums[p2]!=nums[p1]) nums[++p1]=nums[p2];
    }
    return numsSize>0 ? p1+1:0;
}

特别简单一道题,给我整的却听够呛

刚开始做是挺多问题的

多练练可能就好了呢~?

榜首人的做法:

代码语言:javascript
复制
int removeDuplicates(int* nums, int numsSize){

    int val = nums[0];
    int index = 1;
    int match_count = 0;
    
    if(numsSize == 0) {
        return 0;
    }
    
    for (int i = 1; i < numsSize; i++) {
        if (val != nums[i]) {
            nums[index] = nums[i];
            index++;
            val = nums[i];
        } else {
            match_count++;
        }
    }
    return numsSize - match_count;
}

方法:双指针法算法

数组完成排序后,我们可以放置两个指针 ii 和 jj,其中 ii 是慢指针,而 jj 是快指针。只要 numsi = numsjnumsi=numsj,我们就增加 jj 以跳过重复项。

当我们遇到 numsj \neq numsinumsj

=numsi 时,跳过重复项的运行已经结束,因此我们必须把它(numsjnumsj)的值复制到 numsi + 1numsi+1。然后递增 ii,接着我们将再次重复相同的过程,直到 jj 到达数组的末尾为止。

Java

public int removeDuplicates(int[] nums) {

代码语言:txt
复制
 if (nums.length == 0) return 0;
代码语言:txt
复制
 int i = 0;
代码语言:txt
复制
 for (int j = 1; j < nums.length; j++) {
代码语言:txt
复制
     if (nums[j] != nums[i]) {
代码语言:txt
复制
         i++;
代码语言:txt
复制
         nums[i] = nums[j];
代码语言:txt
复制
     }
代码语言:txt
复制
 }
代码语言:txt
复制
 return i + 1;

}

复杂度分析

时间复杂度:O(n)O(n),假设数组的长度是 nn,那么 ii 和 jj 分别最多遍历 nn 步。

空间复杂度:O(1)O(1)。

作者:LeetCode

链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-xiang-by-/

来源:力扣(LeetCode)

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 辣鸡代码
  • 特别简单一道题,给我整的却听够呛
  • 刚开始做是挺多问题的
    • 多练练可能就好了呢~?
    • 榜首人的做法:
    • 方法:双指针法算法
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档