Leetcode 26 Remove Duplicates

举报
herosunly 发表于 2021/11/18 23:16:24 2021/11/18
【摘要】 Leetcode 26 Remove Duplicates 题目描述 Remove Duplicates from Sorted Array Given a sorted array, remove ...

Leetcode 26 Remove Duplicates

题目描述

Remove Duplicates from Sorted Array
Given a sorted array, 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 in place with constant memory.

For example,
Given input array 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 new length.

解题思路

此题亦可以使用插空法,即插入到正确的位置。具体来说的话,当遍历到的元素和插空前的元素进行比较,如果不等于,直接插入,如果相等,则跳过即可。

代码实现

C++版本

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int len_nums = nums.size();

        if (len_nums <= 0) {
            return 0;
        }

        int insert_pos = 1;
        int i = 1;

        while (i < len_nums) {
            if (nums[i] != nums[insert_pos - 1]) {
                nums[insert_pos++] = nums[i++];
            }

            else {
                ++i;
            }
        }

        return insert_pos;
    }
};

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

Java版本

public class Solution {
    public int removeDuplicates(int[] nums) {
       int len = nums.length;
       if (len <= 0) {
            return 0;
       }

       int i = 1;
       int insert_pos = 1;

       while (i < len) {
            if (nums[i] != nums[insert_pos - 1]) {
                nums[insert_pos++] = nums[i++];
            }

            else {
                ++i;
            }
       }

       return insert_pos;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Python版本

class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        len_nums = len(nums)
        if len_nums <= 0:
            return 0

        insert_pos = 1
        i = 1

        while i < len_nums:
            if nums[i] != nums[insert_pos - 1]:
                nums[insert_pos] = nums[i]
                insert_pos += 1
                i += 1

            else:
                i += 1

        return insert_pos

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

文章来源: blog.csdn.net,作者:herosunly,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/herosunly/article/details/86651163

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。