|
2565 | 2565 | "sort-colors":{
|
2566 | 2566 | "id": "75",
|
2567 | 2567 | "name": "sort-colors",
|
2568 |
| - "pre": [], |
2569 |
| - "keyPoints": [], |
2570 |
| - "companies": [], |
| 2568 | + "pre": [ |
| 2569 | + { |
| 2570 | + "text": "荷兰国旗问题", |
| 2571 | + "link": "https://en.wikipedia.org/wiki/Dutch_national_flag_problem", |
| 2572 | + "color": "purple" |
| 2573 | + }, |
| 2574 | + { |
| 2575 | + "text": "排序", |
| 2576 | + "link": null, |
| 2577 | + "color": "purple" |
| 2578 | + } |
| 2579 | + ], |
| 2580 | + "keyPoints": [ |
| 2581 | + { |
| 2582 | + "text": "荷兰国旗问题", |
| 2583 | + "link": null, |
| 2584 | + "color": "blue" |
| 2585 | + }, |
| 2586 | + { |
| 2587 | + "text": "countingsort", |
| 2588 | + "link": null, |
| 2589 | + "color": "blue" |
| 2590 | + } |
| 2591 | + ], |
| 2592 | + "companies": [ |
| 2593 | + { |
| 2594 | + "name": "阿里巴巴" |
| 2595 | + }, |
| 2596 | + { |
| 2597 | + "name": "腾讯" |
| 2598 | + }, |
| 2599 | + { |
| 2600 | + "name": "百度" |
| 2601 | + }, |
| 2602 | + { |
| 2603 | + "name": "字节跳动" |
| 2604 | + } |
| 2605 | + ], |
2571 | 2606 | "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/75.sort-colors.md",
|
2572 | 2607 | "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/75.sort-colors.md",
|
2573 |
| - "code": [] |
| 2608 | + "code": [ |
| 2609 | + { |
| 2610 | + "language": "cpp", |
| 2611 | + "text": "\nclass Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int r = 0, g = 0, b = 0;\n for (int n : nums) {\n if (n == 0) {\n nums[b++] = 2;\n nums[g++] = 1;\n nums[r++] = 0;\n } else if (n == 1) {\n nums[b++] = 2;\n nums[g++] = 1;\n } else nums[b++] = 2;\n }\n }\n};\n" |
| 2612 | + }, |
| 2613 | + { |
| 2614 | + "language": "py", |
| 2615 | + "text": "\nclass Solution:\n def sortColors(self, strs):\n # p0 是右边界\n # p1 是右边界\n # p2 是���边界\n # p1 超过 p2 结束\n p0, p1, p2 = 0, 0, len(strs) - 1\n\n while p1 <= p2:\n if strs[p1] == 'blue':\n strs[p2], strs[p1] = strs[p1], strs[p2]\n p2 -= 1\n elif strs[p1] == 'red':\n strs[p0], strs[p1] = strs[p1], strs[p0]\n p0 += 1\n p1 += 1 # p0 一定不是 blue,因此 p1 += 1\n else: # p1 === 'green'\n p1 += 1\n return strs\n" |
| 2616 | + }, |
| 2617 | + { |
| 2618 | + "language": "py", |
| 2619 | + "text": "\nclass Solution:\n def partition(self, head: ListNode, x: int) -> ListNode:\n l1 = cur = head\n while cur:\n if cur.val < x:\n cur.val, l1.val = l1.val, cur.val\n l1 = l1.next\n cur = cur.next\n return head\n" |
| 2620 | + } |
| 2621 | + ] |
2574 | 2622 | },
|
2575 | 2623 | "subsets":{
|
2576 | 2624 | "id": "78",
|
@@ -10686,12 +10734,34 @@
|
10686 | 10734 | "snakes-and-ladders":{
|
10687 | 10735 | "id": "909",
|
10688 | 10736 | "name": "snakes-and-ladders",
|
10689 |
| - "pre": [], |
10690 |
| - "keyPoints": [], |
| 10737 | + "pre": [ |
| 10738 | + { |
| 10739 | + "text": "广度优先遍历", |
| 10740 | + "link": null, |
| 10741 | + "color": "gold" |
| 10742 | + } |
| 10743 | + ], |
| 10744 | + "keyPoints": [ |
| 10745 | + { |
| 10746 | + "text": "根据矩阵编号如何算出其都在的行号和列号。这里其实用到了number=(row", |
| 10747 | + "link": null, |
| 10748 | + "color": "blue" |
| 10749 | + }, |
| 10750 | + { |
| 10751 | + "text": "1)\\*n+col这样的一个公式,后面的所有公式都是基于它产生的。", |
| 10752 | + "link": null, |
| 10753 | + "color": "blue" |
| 10754 | + } |
| 10755 | + ], |
10691 | 10756 | "companies": [],
|
10692 | 10757 | "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/909.snakes-and-ladders.md",
|
10693 | 10758 | "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/909.snakes-and-ladders.md",
|
10694 |
| - "code": [] |
| 10759 | + "code": [ |
| 10760 | + { |
| 10761 | + "language": "py", |
| 10762 | + "text": "\n\nclass Solution:\n def snakesAndLadders(self, board: List[List[int]]) -> int:\n q = collections.deque([(1, 0)])\n n = len(board)\n visited = set()\n\n def get_pos(pos):\n row = (n - 1) - (pos - 1) // n\n col = (n - 1) - ((pos - 1) % n) if row & 1 == n & 1 else (pos - 1) % n\n return row, col\n\n while q:\n for _ in range(len(q)):\n cur, steps = q.popleft()\n if cur in visited:\n continue\n visited.add(cur)\n if cur == n ** 2:\n return steps\n for nxt in range(cur + 1, min(cur + 6, n * n) + 1):\n row, col = get_pos(nxt)\n if board[row][col] == -1:\n q.append((nxt, steps + 1))\n else:\n q.append((board[row][col], steps + 1))\n return -1\n\n" |
| 10763 | + } |
| 10764 | + ] |
10695 | 10765 | },
|
10696 | 10766 | "online-election":{
|
10697 | 10767 | "id": "911",
|
|
0 commit comments