|
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",
|
|
4516 | 4564 | {
|
4517 | 4565 | "language": "cpp",
|
4518 | 4566 | "text": "\nclass Solution {\npublic:\n bool wordBreak(string s, vector<string>& dict) {\n unordered_set<string> st(begin(dict), end(dict));\n int N = s.size();\n vector<bool> dp(N + 1);\n dp[0] = true;\n for (int i = 1; i <= N; ++i) {\n for (int j = 0; j < i && !dp[i]; ++j) {\n dp[i] = dp[j] && st.count(s.substr(j, i - j));\n }\n }\n return dp[N];\n }\n};\n\n"
|
| 4567 | + }, |
| 4568 | + { |
| 4569 | + "language": "py", |
| 4570 | + "text": "\n@cache\ndef dp(pos):\n if pos == len(s): return True\n for word in wordDict:\n if s[pos:pos+len(word)] == word and dp(pos + len(word)): return True\n return False\nreturn dp(0)\n" |
| 4571 | + }, |
| 4572 | + { |
| 4573 | + "language": "py", |
| 4574 | + "text": "\nclass Solution:\n def wordBreak(self, s: str, wordDict: List[str]) -> bool:\n wordDict = set(wordDict)\n @cache\n def dp(pos):\n if pos == len(s): return True\n cur = ''\n for nxt in range(pos, len(s)):\n cur += s[nxt]\n if cur in wordDict and dp(nxt + 1): return True\n return False\n return dp(0)\n" |
4519 | 4575 | }
|
4520 | 4576 | ]
|
4521 | 4577 | },
|
|
5619 | 5675 | "remove-linked-list-elements":{
|
5620 | 5676 | "id": "203",
|
5621 | 5677 | "name": "remove-linked-list-elements",
|
5622 |
| - "pre": [], |
5623 |
| - "keyPoints": [], |
5624 |
| - "companies": [], |
| 5678 | + "pre": [ |
| 5679 | + { |
| 5680 | + "text": "链表", |
| 5681 | + "link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/basic-data-structure.md", |
| 5682 | + "color": "magenta" |
| 5683 | + } |
| 5684 | + ], |
| 5685 | + "keyPoints": [ |
| 5686 | + { |
| 5687 | + "text": "链表的基本操作(删除指定节点)", |
| 5688 | + "link": null, |
| 5689 | + "color": "blue" |
| 5690 | + }, |
| 5691 | + { |
| 5692 | + "text": "虚拟节点dummy简化操作>其实设置dummy节点就是为了处理特殊位置(头节点),这这道题就是如果头节点是给定的需要删除的节点呢?>为了保证代码逻辑的一致性,即不需要为头节点特殊定制逻辑,才采用的虚拟节点。", |
| 5693 | + "link": null, |
| 5694 | + "color": "blue" |
| 5695 | + }, |
| 5696 | + { |
| 5697 | + "text": "如果连续两个节点都是要删除的节点,这个情况容易被忽略。eg:```js//只有下个节点不是要删除的节点才更新currentif(!next||next.val!==val){current=next;}```", |
| 5698 | + "link": null, |
| 5699 | + "color": "blue" |
| 5700 | + } |
| 5701 | + ], |
| 5702 | + "companies": [ |
| 5703 | + { |
| 5704 | + "name": "阿里巴巴" |
| 5705 | + }, |
| 5706 | + { |
| 5707 | + "name": "腾讯" |
| 5708 | + }, |
| 5709 | + { |
| 5710 | + "name": "百度" |
| 5711 | + }, |
| 5712 | + { |
| 5713 | + "name": "字节跳动" |
| 5714 | + } |
| 5715 | + ], |
5625 | 5716 | "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/203.remove-linked-list-elements.md",
|
5626 | 5717 | "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/203.remove-linked-list-elements.md",
|
5627 |
| - "code": [] |
| 5718 | + "code": [ |
| 5719 | + { |
| 5720 | + "language": "java", |
| 5721 | + "text": "\nclass Solution {\n public ListNode removeElements(ListNode head, int val) {\n ListNode dummyHead = new ListNode(0);\n dummyHead.next = head;\n ListNode temp = dummyHead;\n while (temp.next != null) {\n if (temp.next.val == val) {\n temp.next = temp.next.next;\n } else {\n temp = temp.next;\n }\n }\n return dummyHead.next;\n }\n}\n" |
| 5722 | + }, |
| 5723 | + { |
| 5724 | + "language": "js", |
| 5725 | + "text": "\n// 只有下个节点不是要删除的节点才更新 current\nif (!next || next.val !== val) {\n current = next;\n}\n" |
| 5726 | + }, |
| 5727 | + { |
| 5728 | + "language": "js", |
| 5729 | + "text": "\n/**\n * @param {ListNode} head\n * @param {number} val\n * @return {ListNode}\n */\nvar removeElements = function (head, val) {\n const dummy = {\n next: head,\n };\n let current = dummy;\n\n while (current && current.next) {\n let next = current.next;\n if (next.val === val) {\n current.next = next.next;\n next = next.next;\n }\n\n if (!next || next.val !== val) {\n current = next;\n }\n }\n\n return dummy.next;\n};\n" |
| 5730 | + }, |
| 5731 | + { |
| 5732 | + "language": "cpp", |
| 5733 | + "text": "\nclass Solution {\npublic:\n ListNode* removeElements(ListNode* head, int val) {\n struct ListNode* dummyHead = new ListNode(0, head);\n struct ListNode* temp = dummyHead;\n while (temp->next != NULL) {\n if (temp->next->val == val) {\n temp->next = temp->next->next;\n } else {\n temp = temp->next;\n }\n }\n return dummyHead->next;\n }\n};\n" |
| 5734 | + }, |
| 5735 | + { |
| 5736 | + "language": "py", |
| 5737 | + "text": "\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def removeElements(self, head: ListNode, val: int) -> ListNode:\n prev = ListNode(0)\n prev.next = head\n cur = prev\n while cur.next:\n if cur.next.val == val:\n cur.next = cur.next.next\n else:\n cur = cur.next\n return prev.next\n" |
| 5738 | + } |
| 5739 | + ] |
5628 | 5740 | },
|
5629 | 5741 | "reverse-linked-list":{
|
5630 | 5742 | "id": "206",
|
@@ -10618,12 +10730,34 @@
|
10618 | 10730 | "snakes-and-ladders":{
|
10619 | 10731 | "id": "909",
|
10620 | 10732 | "name": "snakes-and-ladders",
|
10621 |
| - "pre": [], |
10622 |
| - "keyPoints": [], |
| 10733 | + "pre": [ |
| 10734 | + { |
| 10735 | + "text": "广度优先遍历", |
| 10736 | + "link": null, |
| 10737 | + "color": "gold" |
| 10738 | + } |
| 10739 | + ], |
| 10740 | + "keyPoints": [ |
| 10741 | + { |
| 10742 | + "text": "根据矩阵编号如何算出其都在的行号和列号。这里其实用到了number=(row", |
| 10743 | + "link": null, |
| 10744 | + "color": "blue" |
| 10745 | + }, |
| 10746 | + { |
| 10747 | + "text": "1)\\*n+col这样的一个公式,后面的所有公式都是基于它产生的。", |
| 10748 | + "link": null, |
| 10749 | + "color": "blue" |
| 10750 | + } |
| 10751 | + ], |
10623 | 10752 | "companies": [],
|
10624 | 10753 | "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/909.snakes-and-ladders.md",
|
10625 | 10754 | "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/909.snakes-and-ladders.md",
|
10626 |
| - "code": [] |
| 10755 | + "code": [ |
| 10756 | + { |
| 10757 | + "language": "py", |
| 10758 | + "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" |
| 10759 | + } |
| 10760 | + ] |
10627 | 10761 | },
|
10628 | 10762 | "online-election":{
|
10629 | 10763 | "id": "911",
|
|
10725 | 10859 | }
|
10726 | 10860 | ]
|
10727 | 10861 | },
|
| 10862 | +"maximum-sum-circular-subarray":{ |
| 10863 | + "id": "918", |
| 10864 | + "name": "maximum-sum-circular-subarray", |
| 10865 | + "pre": [ |
| 10866 | + { |
| 10867 | + "text": "动态规划", |
| 10868 | + "link": null, |
| 10869 | + "color": "red" |
| 10870 | + } |
| 10871 | + ], |
| 10872 | + "keyPoints": [ |
| 10873 | + { |
| 10874 | + "text": "其中一种情况(两段子序和):转化为sum(nums)", |
| 10875 | + "link": null, |
| 10876 | + "color": "blue" |
| 10877 | + }, |
| 10878 | + { |
| 10879 | + "text": "最小子序和", |
| 10880 | + "link": null, |
| 10881 | + "color": "blue" |
| 10882 | + } |
| 10883 | + ], |
| 10884 | + "companies": [], |
| 10885 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/918.maximum-sum-circular-subarray.md", |
| 10886 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/918.maximum-sum-circular-subarray.md", |
| 10887 | + "code": [ |
| 10888 | + { |
| 10889 | + "language": "py", |
| 10890 | + "text": "\n\nclass Solution:\n # 最小子序和\n def solve1(self, A):\n A = A\n dp = [inf] * len(A)\n for i in range(len(A)):\n dp[i] = min(A[i], dp[i - 1] + A[i])\n return min(dp)\n # 最大子序和\n def solve2(self, A):\n A = A\n dp = [-inf] * len(A)\n for i in range(len(A)):\n dp[i] = max(A[i], dp[i - 1] + A[i])\n return max(dp)\n def maxSubarraySumCircular(self, nums: List[int]) -> int:\n ans1 = sum(nums) - self.solve1(nums)\n ans2 = self.solve2(nums)\n if ans1 == 0: ans1 = max(nums) # 不能为空,那就选一个最大的吧\n return max(ans1, ans2)\n\n" |
| 10891 | + } |
| 10892 | + ] |
| 10893 | +}, |
10728 | 10894 | "beautiful-array":{
|
10729 | 10895 | "id": "932",
|
10730 | 10896 | "name": "beautiful-array",
|
|
11460 | 11626 | }
|
11461 | 11627 | ]
|
11462 | 11628 | },
|
| 11629 | +"previous-permutation-with-one-swap":{ |
| 11630 | + "id": "1053", |
| 11631 | + "name": "previous-permutation-with-one-swap", |
| 11632 | + "pre": [], |
| 11633 | + "keyPoints": [ |
| 11634 | + { |
| 11635 | + "text": "需要i尽可能地大(尽可能的把低位变大,而不是高位),nums[j]尽可能大", |
| 11636 | + "link": null, |
| 11637 | + "color": "blue" |
| 11638 | + } |
| 11639 | + ], |
| 11640 | + "companies": [], |
| 11641 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/1053.previous-permutation-with-one-swap.md", |
| 11642 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/1053.previous-permutation-with-one-swap.md", |
| 11643 | + "code": [ |
| 11644 | + { |
| 11645 | + "language": "py", |
| 11646 | + "text": "\n\nclass Solution:\n def prevPermOpt1(self, arr: List[int]) -> List[int]:\n l = -1\n for i in range(len(arr)-1, -1, -1):\n if arr[i-1] > arr[i]:\n l = i - 1\n break\n if l == -1: return arr\n ans = 0\n r = -1\n for i in range(l+1, len(arr)):\n if arr[i] < arr[l] and arr[i] > ans:\n ans = arr[i]\n r = i\n if r == -1:\n return arr\n arr[l], arr[r] = arr[r], arr[l]\n return arr\n \n" |
| 11647 | + }, |
| 11648 | + { |
| 11649 | + "language": "py", |
| 11650 | + "text": "\n\nclass Solution:\n def prevPermOpt1(self, arr: List[int]) -> List[int]:\n l = -1\n for i in range(len(arr)-1, -1, -1):\n if arr[i-1] > arr[i]:\n l = i - 1\n break\n if l == -1: return arr\n for i in range(len(arr)-1, l, -1):\n if arr[i] < arr[l] and arr[i] != arr[i-1]:\n r = i\n break\n if r == -1:\n return arr\n arr[l], arr[r] = arr[r], arr[l]\n return arr\n \n \n\n" |
| 11651 | + } |
| 11652 | + ] |
| 11653 | +}, |
11463 | 11654 | "path-in-zigzag-labelled-binary-tree":{
|
11464 | 11655 | "id": "1104",
|
11465 | 11656 | "name": "path-in-zigzag-labelled-binary-tree",
|
|
12797 | 12988 | }
|
12798 | 12989 | ]
|
12799 | 12990 | },
|
| 12991 | +"count-substrings-that-differ-by-one-character":{ |
| 12992 | + "id": "1638", |
| 12993 | + "name": "count-substrings-that-differ-by-one-character", |
| 12994 | + "pre": [ |
| 12995 | + { |
| 12996 | + "text": "枚举", |
| 12997 | + "link": null, |
| 12998 | + "color": "magenta" |
| 12999 | + }, |
| 13000 | + { |
| 13001 | + "text": "递推", |
| 13002 | + "link": null, |
| 13003 | + "color": "volcano" |
| 13004 | + }, |
| 13005 | + { |
| 13006 | + "text": "动态规划", |
| 13007 | + "link": null, |
| 13008 | + "color": "red" |
| 13009 | + } |
| 13010 | + ], |
| 13011 | + "keyPoints": [ |
| 13012 | + { |
| 13013 | + "text": "枚举s和t的起点i和j,接下来枚举子串长度k", |
| 13014 | + "link": null, |
| 13015 | + "color": "blue" |
| 13016 | + } |
| 13017 | + ], |
| 13018 | + "companies": [], |
| 13019 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/1638.count-substrings-that-differ-by-one-character.md", |
| 13020 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/1638.count-substrings-that-differ-by-one-character.md", |
| 13021 | + "code": [ |
| 13022 | + { |
| 13023 | + "language": "py", |
| 13024 | + "text": "\n\n# 方法 1\nclass Solution:\n def countSubstrings(self, s: str, t: str) -> int:\n m, n = len(s), len(t)\n ans = 0\n for i in range(m):\n for j in range(n):\n diff = 0\n k = 0\n while i + k < m and j + k < n:\n diff += int(s[i + k] != t[j + k])\n if diff > 1:\n break\n if diff == 1:\n ans += 1\n k += 1\n return ans\n\n" |
| 13025 | + }, |
| 13026 | + { |
| 13027 | + "language": "py", |
| 13028 | + "text": "\n\n# 方法 2\nclass Solution:\n def countSubstrings(self, s: str, t: str) -> int:\n L = [[0] * (len(t)+1) for _ in range(len(s)+1)] # L[i][j] 表示 s[i] != s[j] 情况下可以向左扩展的最大长度\n R = [[0] * (len(t)+1) for _ in range(len(s)+1)] # R[i][j] 表示 s[i] != s[j] 情况下可以向右扩展的最大长度\n ans = 0\n for i in range(1,len(s)+1):\n for j in range(1,len(t)+1):\n if s[i-1] != t[j-1]:\n L[i][j] = 0\n else:\n L[i][j] = L[i-1][j-1] + 1\n for i in range(len(s)-1,-1,-1):\n for j in range(len(t)-1,-1,-1):\n if s[i] != t[j]:\n R[i][j] = 0\n else:\n R[i][j] = R[i+1][j+1] + 1\n # 枚举不同的那个字符,这样就只需向左向右匹配即可\n for i in range(len(s)):\n for j in range(len(t)):\n # L 前面有哨兵,因此 L[i][j] 相当于没有哨兵的 L[i-1][j-1]\n if s[i] != t[j]: ans += (L[i][j] + 1) * (R[i+1][j+1] + 1)\n return ans\n\n" |
| 13029 | + } |
| 13030 | + ] |
| 13031 | +}, |
12800 | 13032 | "number-of-ways-to-form-a-target-string-given-a-dictionary":{
|
12801 | 13033 | "id": "1639",
|
12802 | 13034 | "name": "number-of-ways-to-form-a-target-string-given-a-dictionary",
|
|
0 commit comments