递归复习:
-
Leetcode 92: Reverse Linked List II
- 解题关键:
- 如何找到对应索引位置的node
for (int i = 1; i < left; i ++) { pre = pre.next; leftNode = leftNode.next; } for (int i = 1; i < right; i ++) { rightNode = rightNode.next; }
- 反转链表的写法
while (leftNode != rightNode) { pre.next = leftNode.next; leftNode.next = rightNode.next; rightNode.next = leftNode; leftNode = pre.next; }
- 解题关键:
-
Leetcode 1863: Sum of All Subset XOR Totals
- 解题关键:
- 如何求XOR值
curr ^ nums[idx]
- 递归遍历找到所有子集,并叠加其XOR totals
findSubSet(nums, idx + 1, curr ^ nums[idx]) + findSubSet(nums, idx + 1, curr)
- 解题关键:
数组
- Leetcode 11: Container With Most Water
- 解题关键:
- 理解most water的计算条件
ret = Math.max(ret, Math.min(height[left], height[right]) * (right - left));
- 解题关键:
- Leetcode 88: Merge Sorted Array
- 解题关键:
- 使用merge sort,可参照Algorithms 4th Edition中Merge sort章节
- 解题关键:
- Leetcode 42: Trapping Water
- 解题关键:
- 同11,理解trapping water成立的条件
- 使用two pointers遍历,将符合条件的情况进行叠加
while (left < right) { if (height[left] < height[right]) { if (height[left] > left_max) { left_max = height[left]; } else { ret += left_max - height[left]; } left ++; } else { if (height[right] > right_max) { right_max = height[right]; } else { ret += right_max - height[right]; } right --; } }
- 解题关键:
二分法
- Leetcode 69: Sqrt(x)
- Leetcode 374: Guess Number Higher or Lower
- Leetcode 378: Kth Smallest Element In a Sorted Matrix
- Leetcode 475: Heaters
- 解题关键
二分法解题关键主要是注意二分法的概念,进而进行相应的遍历操作,例如叠加,overwrite