10 Regular Expression Match
- Back Tracking
- boundary condition: sLen == 0 || pLen == 0
- then deal with
p.charAt(1) == '*'
andother
1 | if(pLen == 0) return sLen == 0; // pLen = a*? ; |
- Dynamic Programming
23 Merge k Sorted Lists
- Heap
1 | ListNode res = new ListNode(0); |
25 Reverse Nodes in k Group
- count and revers
1 | for(int i = 0; i < k; i++) { |
33 Search in Rotated Array
- Binary Search
- find the realMin
- to get the related position of nodes in Rotated Array by
realMid = (mid + realMin) % nums.length
- use mid
else end = mid - 1; // use mid
1 | while(start < end) { // get the real mid |
43 Multiply String
- String
- use
int[] prod = new int[len1 + len2];
to store each production of every two digits - two for loops to compute prod
- convert prod to string, from end to begin
// when go through from end, must use i = len-1 and i--
- remove ‘0’ at the beginning of result, but don’t remove the last ‘0’
- use
49 Group Anagrams
- Question: output format
1 | for(int j = 0; j < strs[i].length(); j++) { |
56 Merge Intervals
1 | if(intervals.get(i).start <= end) { // <= |
##76 Minimum Window Substring
- Hash Table, use int[128] to store remaining char
1 | int start = 0, end = 0, min = Integer.MAX_VALUE, minStart = 0; |
117 Populating Next Right Pointers in Each Node II
-Dummy point at left of leftmost node
1 | while(root != null) { // |
133 Clone Graph
- DFS + map
- use map to denote whether a node has been visited
1 | return map.get(node.label); // must return what we added early, if we return node, would be a reference of original graph |
158 Read N Characters Given Read4 II - Call multiple times
- Two pointer
1 | int bufCnt = 0; |
161 One Edit Distance
1 | return s.substring(i).equals(t.substring(j+1)) || s.substring(i+1).equals(t.substring(j+1)); |
200 Number of Islands
1 | // 第一次'0'的单引号漏掉了,找了很久才发现!!! |
215 Kth Largest Element in an Array
- D ivide and Conquer
- 每次更新begin 和 end之后要拿begin和end来做边界条件,而不是0和 nums.length - 1 来做边界,这一点在其他时候也要注意,边界条件尽量不要用硬编码
- don’t miss the boundary condition,if(left == right) return nums[left];
- Heap
- PriorityQueue
pq = new PriorityQueue (k+1); // add(), poll();
221 Maximal Square
- DP
- 有两个矩阵,一个表示路,一个用来计数,当需要判断有没有路并在计数上叠加时很容易比错矩阵
- 优化:开一个大一层的计数矩阵来计数就可以不用初始化计数矩阵的上左两边了
253 Meeting Rooms II
- Heap
1 | Arrays.sort(intervals, new Comparator<Interval>() { |
261 Graph Valid Tree
- Union Find
1 | return n == edges.length + 1; // not always rigth |
- DFS & BFS
265 Paint House II
1 | introduce two min: min1 min2 |
269 Alien Dictionary
- Topological Sort
1 | List<int[]> edges = new LinkedList<int[]>(); |
149 Max Points on a Line
Map<x, Map<y, n>>
<dx, dy>
to identify whether on the same line, in order to deal with vertical situation- overlap variable deal with overlap situation
1 | if(x == 0 && y == 0) { // |
res = Math.max(res, max + overlap + 1); // +1
count the node itself