6.5
-
fibonacci-number (easy)
最基础的算斐波那契数列。递归可以,或者是最简单的动态规划。这里的优化就是空间复杂度从O(n)到o(1) -
non-decreasing-array (easy)
不是要你判断non decreasing,而是at most 1 change之后non decreasing。
不能简单地考虑一个局部地斜率为负数,这不足以解决问题。
提示: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:这里引入两个好用的函数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
很简单,但是最后看结果的时候,不是小于,也可能是大于,要考虑。 -
self-dividing-numbers (easy)
128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0
这个题一开始没有思路,但其实就是把数学思路实现一遍。需要一个辅助变量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
网友评论