题目 第一次刷题 第二次刷题 第三次刷题

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]

|
- [ ]

|
- [ ]

| | |
- [ ]

|
- [ ]

|
- [ ]

| | |
- [ ]

|
- [ ]

|
- [ ]

| | |
- [ ]

|
- [ ]

|
- [ ]

| | |
- [ ]

|
- [ ]

|
- [ ]

| | |
- [ ]

|
- [ ]

|
- [ ]

|

image.png

image.png

分治

  1. class Solution {
  2. public ListNode mergeKLists(ListNode[] lists) {
  3. return merge(lists, 0, lists.length - 1);
  4. }
  5. public ListNode merge(ListNode[] lists, int l, int r) {
  6. if (l == r) {
  7. return lists[l];
  8. }
  9. if (l > r) {
  10. return null;
  11. }
  12. int mid = (l + r) >> 1;
  13. return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
  14. }
  15. public ListNode mergeTwoLists(ListNode a, ListNode b) {
  16. if (a == null || b == null) {
  17. return a != null ? a : b;
  18. }
  19. ListNode head = new ListNode(0);
  20. ListNode tail = head, aPtr = a, bPtr = b;
  21. while (aPtr != null && bPtr != null) {
  22. if (aPtr.val < bPtr.val) {
  23. tail.next = aPtr;
  24. aPtr = aPtr.next;
  25. } else {
  26. tail.next = bPtr;
  27. bPtr = bPtr.next;
  28. }
  29. tail = tail.next;
  30. }
  31. tail.next = (aPtr != null ? aPtr : bPtr);
  32. return head.next;
  33. }
  34. }

递归我们应该关心的主要有三点:

  1. 返回值
  2. 调用单元做了什么
  3. 终止条件
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;
    }
}