26. Remove Duplicates from Sorted Array

Posted jyg694234697

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26. 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.

技术分享图片

 

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:

 

class Solution(object):
    def removeDuplicates(self, A):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not A:
            return 0

        newTail = 0

        for i in range(1, len(A)):
            if A[i] != A[newTail]:
                newTail += 1
                A[newTail] = A[i]

        return newTail + 1

 

 

以上

以上是关于26. Remove Duplicates from Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array

#26 Remove Duplicates from Sorted Array

LeetCode OJ 26. Remove Duplicates from Sorted Array

leetcode 26. Remove Duplicates from Sorted Array 80. Remove Duplicates from Sorted Array II

26. Remove Duplicates from Sorted Arrayeasy