LintCode_chapter2_section11_remove-duplicates-from-sorted-array-ii

coding = utf-8

'''
Created on 2015年11月10日

@author: SphinxW
'''
# 刪除排序數組中的重復數字 II
#
# 跟進“刪除重復數字”:
#
# 如果可以允許出現兩次重復將如何處理?
#
#
# 樣例
#
# 給出數組A =[1,1,1,2,2,3],你的函數應該返回長度5,此時A=[1,1,2,2,3]。


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

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

推薦閱讀更多精彩內容