如果是有關string的題目,多半是講求效率
而最好的效率應該就是linear time(至少在類似interview problems中)
至於buffer也是能省就省,避免allocate unnecessary space
所以值得注意兩點:
1. Avoid redundant shift due to character removal
2. Use ASCII array (by default definition) instead of character comparison
此外也可以用swap頭尾兩端的方法來reverse a string
題外話:dynamic programming可看作是recursion with caching
避免重覆計算一樣的東西
Algorithm design should consider both (1) base case and (2) recursive case
By the way, recursion can be inefficient while filling the call stack by explicitly returning desired answers. see more from Tail recursion.
沒有留言:
張貼留言