每天(?)一道LeetCode(2) Remove Duplicates from Sorted Array

Array

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.
不是很理解:
不能为另外一个数组分配额外的空间,必须使用常量内存完成。

Solutions

利用两个指针

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        j = 0
        i = 1
        while i

下面这个算法有点儿没看明白,明天再看下

    def removeDuplicates1(self, A):
        if not A:
            return 0

        last = 0
        for i in range(len(A)):
            if A[last] != A[i]:
                last += 1
                A[last] = A[i]
        return last + 1

你可能感兴趣的:(每天(?)一道LeetCode(2) Remove Duplicates from Sorted Array)