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