Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
雖然題目無寫,如果做唔到inplace既話,呢條問題都無乜意思
小弟第一個approach用左…兩支pointer,一支指住頭一支指住the last element
頭pointer一路向前郁,見到有target就swap with last element
當然要check埋last element 係咪都係target
然後尾pointer向前郁
until 頭尾pointer相遇
聽落幾容易,但係去到D boundary case我就死左=.=
即係頭尾pointer相遇果下,唔洗swap, 咁究應該return pointer-1, pointer, 定pointer+1既array?
又要諗下如果array lenght係0/1果D情況…
搞左好耐都搞唔掂=.=
基本上呢個唔係algorithm既問題,只係programming 問題
上網睇人做,睇左幾個,得一個係好似我咁swap
其他都係用兩支pointer, 兩支一開始都係支住個頭
一支就每round都郁, 另一支係check到唔中target先郁
所以pointer 1永遠都ahead of pointer 2, 如果pointer 2督中左target, 就將pointer 1督住果個抄落去
pointer 1去到尾即係loop曬成個array, 呢個時候pointer 2就指住左end of array





Recent Comments