6.5
fibonacci-number (easy)
最基礎的算斐波那契數(shù)列螟深。遞歸可以姨伟,或者是最簡單的動態(tài)規(guī)劃践惑。這里的優(yōu)化就是空間復雜度從O(n)到o(1)non-decreasing-array (easy)
不是要你判斷non decreasing厢漩,而是at most 1 change之后non decreasing。
不能簡單地考慮一個局部地斜率為負數(shù)籽孙,這不足以解決問題。
提示:greedy
This problem is like a greedy problem. When you find nums[i-1] > nums[i] for some i, you will prefer to change nums[i-1]'s value, since a larger nums[i] will give you more risks that you get inversion errors after position i. But, if you also find nums[i-2] > nums[i], then you have to change nums[i]'s value instead, or else you need to change both of nums[i-2]'s and nums[i-1]'s values.
di-string-match (easy)
Return any permutation of [0, 1, ..., N] for given directive (decrease or increase)
左右各定義一個變量才是核心火俄,int left = 0, right = S.length();
要遞增犯建,自然是從最小的開始遞增,反之亦然瓜客。
6.6
first-bad-version (easy)
二分法适瓦。
這里有一個很極端的例子,start + 1 < end這個判斷條件中谱仪,start+1因為start過大玻熙,而溢出了,所以不可以使用疯攒。變成start < end - 1 即可嗦随。valid-palindrome (easy)
回文。 considering only alphanumeric characters敬尺。
stack理論是可以的枚尼,這里用in place two pointers兩邊逼近即可贴浙。
要點1:這里引入兩個好用的函數(shù)Character.isLetterOrDigit 和 Character.toLowerCase
要點2:while里面套while的時候,里面的while注意也要符合外面while的條件姑原,這樣才不會不符合實際問題的需求悬而。remove-linked-list-elements (easy)
這種remove操作,一個pointer是不夠的锭汛,需要兩個笨奠,一個curr一個prev。
而prev和current都需要向尾部推進唤殴。
6.10
fixed-point (easy)
給一個不重復的升序序列般婆,return the smallest index i that satisfies A[i] == i
很簡單,但是最后看結(jié)果的時候朵逝,不是小于蔚袍,也可能是大于,要考慮配名。self-dividing-numbers (easy)
128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0
這個題一開始沒有思路啤咽,但其實就是把數(shù)學思路實現(xiàn)一遍。需要一個輔助變量j渠脉。
6.11
- number-of-recent-calls (easy)
Write a class RecentCounter to count recent requests
6.29
按類別刷題來攻克:
- Array
- String
- Math
- Tree
- BackTracking
- DP
- Linked list
- Binary Search
- Matrix
- DFS & BFS
- Stack & PriorityQueue
- Bit Manipulation
- Topological Sort
- Random
- Graph
- Union Find
- Trie
- Design