刷题还是得多反思复盘总结…开个post吧
先把之前写的一些奇奇怪怪的东西贴过来:
Read questions carefully.
Matrix
how to flatten a 2d matrix?
2d matrix <-> 1d array
1 | M[i][j] = M[n*i+j] |
n: column of the matrix
search in a matrix
Some matrix have descending/non-descending
row or columns.
In such cases, we can:
- use
binary search
. - assign a
cursor
. - …
Two Pointers
Intervals - looking for boundaries
1 | int start = Math.max(1, 2); |
Sliding window
0,0 -> 0,1 … 0,size
for(right)
right go ahead, make window
then move left
Tree
postorderTraversal
postorderTraversal is opposite to preorderTraversal, so we can put preorderTraversal to a stack, then pop the postorderTraversal.
BST
sorted array => BST
middle element is the root node
backtrack
use a linkedlist as track or use arguments
like tree traversal, when we get the base case, add result then return.
1 | we add track(make decision) |
Strings / Chars
1 | int[ ] hash = new int[26/256]; |
think reversely sometimes helps.
- iterating from the smallest? or largest?
- if A->B is difficult, how about use B to verify A?
Greedy (??????)
Prefix Sum
curSum - prevSum = k (prev, cur)
curSum - k = prevSum
min
secondMin