|
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",
|
|
13782 | 13830 | "text": "反向思考,题目要找最少操作数,其实就是找最多保留多少个数",
|
13783 | 13831 | "link": null,
|
13784 | 13832 | "color": "blue"
|
| 13833 | + }, |
| 13834 | + { |
| 13835 | + "text": "对于每一个num我们需要找到其作为左端点时,那么右端点就是v+on", |
| 13836 | + "link": null, |
| 13837 | + "color": "blue" |
| 13838 | + }, |
| 13839 | + { |
| 13840 | + "text": "1,于是我们在这个数组中找值在num和v+on", |
| 13841 | + "link": null, |
| 13842 | + "color": "blue" |
| 13843 | + }, |
| 13844 | + { |
| 13845 | + "text": "1的有多少个,这些都是可以保留的", |
| 13846 | + "link": null, |
| 13847 | + "color": "blue" |
| 13848 | + }, |
| 13849 | + { |
| 13850 | + "text": "排序+二分减少时间复杂度", |
| 13851 | + "link": null, |
| 13852 | + "color": "blue" |
13785 | 13853 | }
|
13786 | 13854 | ],
|
13787 | 13855 | "companies": [],
|
|
13790 | 13858 | "code": [
|
13791 | 13859 | {
|
13792 | 13860 | "language": "py",
|
13793 |
| - "text": "\n\nimport bisect\n\n\nclass Solution:\n def minOperations(self, nums: List[int]) -> int:\n ans = on = len(nums)\n nums = list(set(nums))\n nums.sort()\n n = len(nums)\n for i, v in enumerate(nums):\n r = bisect.bisect_right(nums, v + on - 1)\n l = bisect.bisect_left(nums, v - on + 1)\n ans = min(ans, n - (r - i), n - (i - l + 1))\n return ans + (on - n)\n\n" |
| 13861 | + "text": "\n\nimport bisect\n\n\nclass Solution:\n def minOperations(self, nums: List[int]) -> int:\n ans = on = len(nums)\n nums = list(set(nums))\n nums.sort()\n n = len(nums)\n for i, v in enumerate(nums):\n # nums[i] 一定有一个是在端点的,如果都不在端点,变成在端点不会使得答案更差\n r = bisect.bisect_right(nums, v + on - 1) # 枚举 i 作为左端点\n l = bisect.bisect_left(nums, v - on + 1) # 枚举 i 作为右端点\n ans = min(ans, n - (r - i), n - (i - l + 1))\n return ans + (on - n)\n\n" |
13794 | 13862 | }
|
13795 | 13863 | ]
|
13796 | 13864 | },
|
|
0 commit comments