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.
For example,
Given input array nums =[1,1,2] ,
Given input array nums =
Your function should return length = 2 , with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
這題的解法想法如下(參考資料)
- 準備一個 index 來存放的最後答案
- 歷遍原本的 vector,檢查是不是和目前 index 前一個數相等,不相等,就塞到目前index的位置
- 歷遍完之後,index 就是長度
c++
kotlin
沒有留言:
張貼留言