[Leetcode]Remove Duplicates from Sorted Array

来源:互联网 发布:通风管道设计绘制软件 编辑:程序博客网 时间:2024/06/06 03:17

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

这题跟Remove Element那题类似, 解法也是差不多的~

class Solution:    # @param a list of integers    # @return an integer    def removeDuplicates(self, A):        if A is None or len(A) == 0: return 0        k = 1        for i in xrange(1, len(A)):            if A[i] == A[i - 1]:                continue            A[k] = A[i]            k += 1        return k


0 0