【題目描述】
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.
給定一個排序數(shù)組,在原數(shù)組中刪除重復出現(xiàn)的數(shù)字,使得每個元素只出現(xiàn)一次,并且返回新的數(shù)組的長度。
不要使用額外的數(shù)組空間,必須在原地沒有額外空間的條件下完成。
【題目鏈接】
www.lintcode.com/en/problem/remove-duplicates-from-sorted-array/
【題目解析】
首先我們需要知道,對于一個排好序的數(shù)組來說,A[N + 1] >= A[N],我們?nèi)匀皇褂脙蓚€游標i和j來處理,假設現(xiàn)在i = j + 1,如果A[i] == A[j],那么我們遞增i,直到A[i] != A[j],這時候我們再設置A[j + 1] = A[i],同時遞增i和j,重復上述過程直到遍歷結(jié)束。
【參考答案】
www.jiuzhang.com/solutions/remove-duplicates-from-sorted-array/