LintCode_chapter2_section3_remove-duplicates-from-sorted-array

coding = utf-8

'''
Created on 2015年11月9日

@author: SphinxW
'''
# 刪除排序數組中的重復數字
#
# 給定一個排序數組,在原數組中刪除重復出現的數字,使得每個元素只出現一次,并且返回新的數組的長度。
#
# 不要使用額外的數組空間,必須在原地沒有額外空間的條件下完成。
# 樣例
#
# 給出數組A =[1,1,2],你的函數應該返回長度2,此時A=[1,2]。


class Solution:
    """
    @param A: a list of integers
    @return an integer
    """

    def removeDuplicates(self, A):
        # write your code here
        index = 1
        if len(A) == 0:
            return None
        fir = A[0]
        while index < len(A):
            if A[index] <= fir:
                del A[index]
            else:
                fir = A[index]
                index += 1
        return len(A)
s = Solution()
A = [1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9]
print(s.removeDuplicates(A))
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容