LintCode Python 簡單級題目 100.刪除排序數組中的重復數字 101.刪除排序數組中的重復數字II


題目100描述:

給定一個排序數組,在原數組中刪除重復出現的數字,使得每個元素只出現一次,並且返回新的數組的長度。

不要使用額外的數組空間,必須在原地沒有額外空間的條件下完成。

樣例

給出數組A =[1,1,2],你的函數應該返回長度2,此時A=[1,2]

標簽 
 

題目101描述:

跟進“刪除重復數字”:

如果可以允許出現兩次重復將如何處理?

 

樣例
 

題目分析:

源碼:

class Solution:
    """
    @param A: a list of integers
    @return an integer
    """
    def removeDuplicates(self, A):
        # write your code here
        i = 0
        while i < len(A)-1:
            if A[i] == A[i+1]:
                A.remove(A[i])
            else:
                i += 1
        return len(A)

 

class Solution:
    """
    @param A: a list of integers
    @return an integer
    """
    def removeDuplicates(self, A):
        # write your code here
        i = 0
        while i < len(A)-2:
            if A[i] == A[i+2]:
                A.remove(A[i])
            else:
                i += 1
        return len(A)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM