| 题目 | 第一次刷题 | 第二次刷题 | 第三次刷题 |
|---|---|---|---|
11. 盛最多水的容器 |
- [x] |
|
- [ ]
|
- [ ]
|
|
283. 移动零
|
- [x]
|
- [ ]
|
- [ ]
|
| 16.25. LRU 缓存 |
- [x]
|
- [ ]
|
- [ ]
|
| 1. 两数之和 |
- [x]
|
- [ ]
|
- [ ]
|
| 15. 三数之和 |
- [x]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
链表
| 题目 | 第一次刷题 | 第二次刷题 | 第三次刷题 |
|---|---|---|---|
19. 删除链表的倒数第 N 个结点 |
- [x] |
|
- [ ]
|
- [ ]
|
|
283. 移动零
|
- [x]
|
- [ ]
|
- [ ]
|
| 16.25. LRU 缓存 |
- [x]
|
- [ ]
|
- [ ]
|
| 1. 两数之和 |
- [x]
|
- [ ]
|
- [ ]
|
| 15. 三数之和 |
- [x]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|
| |
- [ ]
|
- [ ]
|
- [ ]
|


分治
class Solution {public ListNode mergeKLists(ListNode[] lists) {return merge(lists, 0, lists.length - 1);}public ListNode merge(ListNode[] lists, int l, int r) {if (l == r) {return lists[l];}if (l > r) {return null;}int mid = (l + r) >> 1;return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));}public ListNode mergeTwoLists(ListNode a, ListNode b) {if (a == null || b == null) {return a != null ? a : b;}ListNode head = new ListNode(0);ListNode tail = head, aPtr = a, bPtr = b;while (aPtr != null && bPtr != null) {if (aPtr.val < bPtr.val) {tail.next = aPtr;aPtr = aPtr.next;} else {tail.next = bPtr;bPtr = bPtr.next;}tail = tail.next;}tail.next = (aPtr != null ? aPtr : bPtr);return head.next;}}
递归我们应该关心的主要有三点:
- 返回值
- 调用单元做了什么
- 终止条件
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
// 这里是返回head
return head;
}
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode pre = new ListNode(0);
pre.next = head;
ListNode temp = pre;
while(temp.next != null && temp.next.next != null) {
ListNode start = temp.next;
ListNode end = temp.next.next;
temp.next = end;
start.next = end.next;
end.next = start;
temp = start;
}
return pre.next;
}
}
