{"id":55,"date":"2026-03-04T16:37:59","date_gmt":"2026-03-04T08:37:59","guid":{"rendered":"https:\/\/sunsyblog.cn\/?p=55"},"modified":"2026-03-16T16:35:49","modified_gmt":"2026-03-16T08:35:49","slug":"leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/","title":{"rendered":"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.<a href=\"https:\/\/leetcode.cn\/problems\/two-sum\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e24\u6570\u4e4b\u548c<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;<code>target<\/code>\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa&nbsp;<strong>\u548c\u4e3a\u76ee\u6807\u503c&nbsp;<\/strong><em><code>target<\/code><\/em>&nbsp; \u7684\u90a3&nbsp;<strong>\u4e24\u4e2a<\/strong>&nbsp;\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\uff0c\u5e76\u4e14\u4f60\u4e0d\u80fd\u4f7f\u7528\u4e24\u6b21\u76f8\u540c\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [2,7,11,15], target = 9<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[0,1]<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u672c\u9898\u4e3b\u8981\u4f7f\u7528\u54c8\u5e0c\u601d\u60f3\uff0c\u904d\u5386nums\u6570\u7ec4\uff0c\u5e76\u67e5\u8be2target-nums[i]\u662f\u5426\u5b58\u5728\u5b57\u5178\u4e2d\uff0c\u82e5\u4e0d\u5b58\u5728\u5219\u5c06\u6b64\u6570\u4f5c\u4e3a\u952e\uff0c\u7d22\u5f15\u4f5c\u4e3a\u503c\u52a0\u5165\u5b57\u5178\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def twoSum(self, nums: List&#91;int], target: int) -&gt; List&#91;int]:\n        pre=dict()\n        for index,num in enumerate(nums):\n            if target-num in pre:\n                return &#91;pre&#91;target-num],index]\n            pre&#91;num]=index\n        return &#91;]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.<a href=\"https:\/\/leetcode.cn\/problems\/group-anagrams\/\" target=\"_blank\"  rel=\"nofollow\" >\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u5206\u7ec4<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u8bf7\u4f60\u5c06&nbsp;\u5b57\u6bcd\u5f02\u4f4d\u8bcd&nbsp;\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7ed3\u679c\u5217\u8868\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165:<\/strong>&nbsp;strs = [\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]<\/p>\n\n\n\n<p><strong>\u8f93\u51fa:&nbsp;<\/strong>[[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]]<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 strs \u4e2d\u6ca1\u6709\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u6392\u5217\u6765\u5f62\u6210&nbsp;<code>\"bat\"<\/code>\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32&nbsp;<code>\"nat\"<\/code>&nbsp;\u548c&nbsp;<code>\"tan\"<\/code>&nbsp;\u662f\u5b57\u6bcd\u5f02\u4f4d\u8bcd\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u91cd\u65b0\u6392\u5217\u4ee5\u5f62\u6210\u5f7c\u6b64\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32&nbsp;<code>\"ate\"<\/code>&nbsp;\uff0c<code>\"eat\"<\/code>&nbsp;\u548c&nbsp;<code>\"tea\"<\/code>&nbsp;\u662f\u5b57\u6bcd\u5f02\u4f4d\u8bcd\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u91cd\u65b0\u6392\u5217\u4ee5\u5f62\u6210\u5f7c\u6b64\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5c06\u5b57\u7b26\u4e32\u5185\u5b57\u6bcd\u6392\u5e8f\u540e\u4f5c\u4e3a\u952e\uff0c\u539f\u5b57\u7b26\u4e32\u4f5c\u4e3a\u503c\uff0c\u518d\u63d0\u53d6\u5b57\u5178\u503c\u5373\u4e3a\u9700\u6c42\u7684\u5217\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def groupAnagrams(self, strs: List&#91;str]) -&gt; List&#91;List&#91;str]]:\n        d=defaultdict(list)\n        for s in strs:\n            sorted_s=''.join(sorted(s))\n            d&#91;sorted_s].append(s)\n        return list(d.values())<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.<a href=\"https:\/\/leetcode.cn\/problems\/longest-consecutive-sequence\/\" target=\"_blank\"  rel=\"nofollow\" >\u6700\u957f\u8fde\u7eed\u5e8f\u5217<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u627e\u51fa\u6570\u5b57\u8fde\u7eed\u7684\u6700\u957f\u5e8f\u5217\uff08\u4e0d\u8981\u6c42\u5e8f\u5217\u5143\u7d20\u5728\u539f\u6570\u7ec4\u4e2d\u8fde\u7eed\uff09\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(n)<\/code><em>&nbsp;<\/em>\u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [100,4,200,1,3,2]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>4<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u957f\u6570\u5b57\u8fde\u7eed\u5e8f\u5217\u662f [1, 2, 3, 4]\u3002\u5b83\u7684\u957f\u5ea6\u4e3a 4\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u9996\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u53bb\u91cd\uff0c\u56e0\u4e3a\u8fde\u7eed\u5e8f\u5217\u4e2d\u4e0d\u4f1a\u91cd\u590d\u3002\u904d\u5386\u6570\u7ec4\uff0c\u901a\u8fc7\u67e5\u627enums[i]-1\u662f\u5426\u5728\u6570\u7ec4\u4e2d\uff0c\u5ffd\u89c6\u4ece\u5e8f\u5217\u4e2d\u95f4\u5f00\u59cb\u7684\u60c5\u51b5\uff0c\u53ea\u4ece\u5e8f\u5217\u5934\u90e8\u5f00\u59cb\u3002\u518d\u5728\u6b64\u57fa\u7840\u4e0a\u5faa\u73af+1\u67e5\u627e\u5c3e\u90e8\u7ec8\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def longestConsecutive(self, nums: List&#91;int]) -&gt; int:\n        st=set(nums)\n        ans=0\n        for i in st:\n            if i-1 in st:\n                continue\n            y=i+1\n            while y in st:\n                y+=1\n            ans=max(ans,y-i)\n        return ans\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.<a href=\"https:\/\/leetcode.cn\/problems\/move-zeroes\/\" target=\"_blank\"  rel=\"nofollow\" >\u79fb\u52a8\u96f6<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u5c06\u6240\u6709&nbsp;<code>0<\/code>&nbsp;\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u672b\u5c3e\uff0c\u540c\u65f6\u4fdd\u6301\u975e\u96f6\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u8bf7\u6ce8\u610f<\/strong>&nbsp;\uff0c\u5fc5\u987b\u5728\u4e0d\u590d\u5236\u6570\u7ec4\u7684\u60c5\u51b5\u4e0b\u539f\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165:<\/strong> nums = <code>[0,1,0,3,12]<\/code><\/li>\n\n\n\n<li><strong>\u8f93\u51fa:<\/strong> <code>[1,3,12,0,0]<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u53cc\u6307\u9488\uff0c\u4e00\u4e2a\u6307\u9488\u6307\u54110\u540e\u7b2c\u4e00\u4e2a\u975e0\u6570\uff0c\u4e00\u4e2a\u6307\u9488\u6307\u5411\u7b2c\u4e00\u4e2a0\uff0c\u901a\u8fc7\u4ea4\u6362\u5c060\u540e\u79fb<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def moveZeroes(self, nums: List&#91;int]) -&gt; None:\n        \"\"\"\n        Do not return anything, modify nums in-place instead.\n        \"\"\"\n        first_0=0\n        first_num=0\n        l_nums=len(nums)\n        while first_num&lt;l_nums:\n            if nums&#91;first_num] != 0:\n                nums&#91;first_0],nums&#91;first_num]=nums&#91;first_num],nums&#91;first_0]\n                first_0 +=1\n            first_num +=1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5.<a href=\"https:\/\/leetcode.cn\/problems\/3sum\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e09\u6570\u4e4b\u548c<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u5224\u65ad\u662f\u5426\u5b58\u5728\u4e09\u5143\u7ec4&nbsp;<code>[nums[i], nums[j], nums[k]]<\/code>&nbsp;\u6ee1\u8db3&nbsp;<code>i != j<\/code>\u3001<code>i != k<\/code>&nbsp;\u4e14&nbsp;<code>j != k<\/code>&nbsp;\uff0c\u540c\u65f6\u8fd8\u6ee1\u8db3&nbsp;<code>nums[i] + nums[j] + nums[k] == 0<\/code>&nbsp;\u3002\u8bf7\u4f60\u8fd4\u56de\u6240\u6709\u548c\u4e3a&nbsp;<code>0<\/code>&nbsp;\u4e14\u4e0d\u91cd\u590d\u7684\u4e09\u5143\u7ec4\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u7b54\u6848\u4e2d\u4e0d\u53ef\u4ee5\u5305\u542b\u91cd\u590d\u7684\u4e09\u5143\u7ec4\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [-1,0,1,2,-1,-4]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[-1,-1,2],[-1,0,1]]<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<p>nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 \u3002<\/p>\n\n\n\n<p>nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 \u3002<\/p>\n\n\n\n<p>nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 \u3002<\/p>\n\n\n\n<p>\u4e0d\u540c\u7684\u4e09\u5143\u7ec4\u662f [-1,0,1] \u548c [-1,-1,2] \u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u8f93\u51fa\u7684\u987a\u5e8f\u548c\u4e09\u5143\u7ec4\u7684\u987a\u5e8f\u5e76\u4e0d\u91cd\u8981\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>1.\u5c06\u6570\u7ec4\u6392\u5e8f<br>2.\u5b9a1\u53d82\uff0c\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u786e\u5b9a\u7b2c\u4e00\u4e2a\u6570\uff0c\u5728\u6b64\u6570\u4e4b\u540e\u7684\u533a\u95f4\u5185\u4f7f\u7528\u53cc\u6307\u9488\u5b9a\u5de6\u53f3\u8fb9\u754c\u4e3a\u8d77\u59cb\u4f4d\uff0c\u4e24\u6307\u9488\u6839\u636e\u4e09\u6570\u503c\u4e0e\u76ee\u6807\u503c\u7684\u5dee\u8ddd\u76f8\u5411\u79fb\u52a8<br>3.\u6dfb\u52a0\u4e00\u4e9b\u8fc7\u6ee4\u6761\u4ef6\uff0c\u51cf\u5c11\u904d\u5386<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def threeSum(self, nums: List&#91;int]) -&gt; List&#91;List&#91;int]]:\n        nums_sort=sorted(nums)\n        res=&#91;]\n        for i in range(len(nums)):\n            if nums_sort&#91;i]&gt;0:\n                return res\n            if i&gt;0 and nums_sort&#91;i]==nums_sort&#91;i-1]:\n                continue\n            left = i+1\n            right= len(nums)-1\n            while left&lt;right:\n                sum_=nums_sort&#91;i]+nums_sort&#91;left]+nums_sort&#91;right]\n                if sum_&gt;0:\n                    right-=1\n                elif sum_&lt;0:\n                    left+=1\n                else :\n                    res.append(&#91;nums_sort&#91;i],nums_sort&#91;left],nums_sort&#91;right]])\n\n                    while right &gt; left and nums_sort&#91;right] == nums_sort&#91;right - 1]:\n                        right -= 1\n                    while right &gt; left and nums_sort&#91;left] == nums_sort&#91;left + 1]:\n                        left += 1\n                    right-=1\n                    left+=1\n        return res<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6.<a href=\"https:\/\/leetcode.cn\/problems\/container-with-most-water\/\" target=\"_blank\"  rel=\"nofollow\" >\u76db\u6700\u591a\u6c34\u7684\u5bb9\u5668<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a&nbsp;<code>n<\/code>&nbsp;\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>height<\/code>&nbsp;\u3002\u6709&nbsp;<code>n<\/code>&nbsp;\u6761\u5782\u7ebf\uff0c\u7b2c&nbsp;<code>i<\/code>&nbsp;\u6761\u7ebf\u7684\u4e24\u4e2a\u7aef\u70b9\u662f&nbsp;<code>(i, 0)<\/code>&nbsp;\u548c&nbsp;<code>(i, height[i])<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u627e\u51fa\u5176\u4e2d\u7684\u4e24\u6761\u7ebf\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e0e&nbsp;<code>x<\/code>&nbsp;\u8f74\u5171\u540c\u6784\u6210\u7684\u5bb9\u5668\u53ef\u4ee5\u5bb9\u7eb3\u6700\u591a\u7684\u6c34\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u5bb9\u5668\u53ef\u4ee5\u50a8\u5b58\u7684\u6700\u5927\u6c34\u91cf\u3002<\/p>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong>\u4f60\u4e0d\u80fd\u503e\u659c\u5bb9\u5668\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>[1,8,6,2,5,4,8,3,7]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>49 <\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u56fe\u4e2d\u5782\u76f4\u7ebf\u4ee3\u8868\u8f93\u5165\u6570\u7ec4 [1,8,6,2,5,4,8,3,7]\u3002\u5728\u6b64\u60c5\u51b5\u4e0b\uff0c\u5bb9\u5668\u80fd\u591f\u5bb9\u7eb3\u6c34\uff08\u8868\u793a\u4e3a\u84dd\u8272\u90e8\u5206\uff09\u7684\u6700\u5927\u503c\u4e3a&nbsp;49\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u53cc\u6307\u9488\u4ece\u4e24\u4fa7\u51fa\u53d1\uff0c\u5411\u4e2d\u95f4\u903c\u8fd1\uff0c\u79fb\u52a8\u4e00\u6b21\u8ba1\u7b97\u4e00\u6b21\u9762\u79ef\uff0c\u8bb0\u5f55\u6700\u5927\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxArea(self, height: List&#91;int]) -&gt; int:\n        l=len(height)\n        left=0\n        right=l-1\n        x=l-1\n        v=min(height&#91;left],height&#91;right])*x\n        while left&lt;right:\n            if height&#91;left]&lt;height&#91;right]:\n                left+=1\n                x-=1\n            else :\n                right-=1\n                x-=1\n            v=max(v,min(height&#91;left],height&#91;right])*x)\n        return v<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7.<a href=\"https:\/\/leetcode.cn\/problems\/trapping-rain-water\/\" target=\"_blank\"  rel=\"nofollow\" >\u63a5\u96e8\u6c34<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a&nbsp;<code>n<\/code>&nbsp;\u4e2a\u975e\u8d1f\u6574\u6570\u8868\u793a\u6bcf\u4e2a\u5bbd\u5ea6\u4e3a&nbsp;<code>1<\/code>&nbsp;\u7684\u67f1\u5b50\u7684\u9ad8\u5ea6\u56fe\uff0c\u8ba1\u7b97\u6309\u6b64\u6392\u5217\u7684\u67f1\u5b50\uff0c\u4e0b\u96e8\u4e4b\u540e\u80fd\u63a5\u591a\u5c11\u96e8\u6c34\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2018\/10\/22\/rainwatertrap.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>6<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u4e0a\u9762\u662f\u7531\u6570\u7ec4 [0,1,0,2,1,0,1,3,2,1,2,1] \u8868\u793a\u7684\u9ad8\u5ea6\u56fe\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u63a5 6 \u4e2a\u5355\u4f4d\u7684\u96e8\u6c34\uff08\u84dd\u8272\u90e8\u5206\u8868\u793a\u96e8\u6c34\uff09\u3002 <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5c06\u6a2a\u8f74\u4e0a\u6bcf\u5355\u4f4d\u95f4\u9694\u770b\u4f5c\u4e00\u4e2a\u72ec\u7acb\u5bb9\u5668\uff0c\u6bcf\u4e2a\u5bb9\u5668\u6709\u4e2a\u5de6\u8fb9\u754c\u3001\u6709\u8fb9\u754c\u4e0e\u5e95\uff0c\u5c06\u6240\u6709\u5bb9\u5668\u5bb9\u91cf\u548c\u52a0\u8d77\u6765\u5373\u4e3a\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5148\u8fdb\u884c\u4e24\u6b21\u904d\u5386\uff0c\u6b63\u5e8f\u904d\u5386\u5f97\u5230\u524d\u5e8f\u6700\u5927\u503cpre_max\u6570\u7ec4\uff08\u5de6\u8fb9\u754c\uff09\uff0c\u540e\u5e8f\u904d\u5386\u5f97\u5230\u540e\u7eed\u6700\u5927\u503c\u6570\u7ec4suf_max\u6570\u7ec4\uff08\u53f3\u8fb9\u754c\uff09\uff0cheight\u6570\u7ec4\u5373\u4e3a\u5e95\u3002<br>\u56e0\u6b64\uff0c\u5355\u4e2a\u5bb9\u5668\u7684\u5bb9\u91cf\u6c42\u89e3\u53ef\u8868\u793a\u4e3a min(\u5de6\u8fb9\u754c\uff0c\u53f3\u8fb9\u754c)-\u5e95 \uff0c\u6839\u636e\u6b64\u904d\u5386height\u5b8c\u6210\u8ba1\u7b97\u6c42\u548c\u5373\u53ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def trap(self, height: List&#91;int]) -&gt; int:\n        n=len(height)\n        pre_max=&#91;0]*n\n        suf_max=&#91;0]*n \n        res=0\n        pre_max&#91;0]=height&#91;0]\n        suf_max&#91;-1]=height&#91;-1]\n        for i in range(1,n):\n            pre_max&#91;i]=max(pre_max&#91;i-1],height&#91;i])\n        for i in range(n-2,-1,-1):\n            suf_max&#91;i]=max(suf_max&#91;i+1],height&#91;i])\n        for i in range(n):\n            res +=min(pre_max&#91;i],suf_max&#91;i])-height&#91;i]\n        return res<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8.<a href=\"https:\/\/leetcode.cn\/problems\/longest-substring-without-repeating-characters\/\" target=\"_blank\"  rel=\"nofollow\" >\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684&nbsp;<strong>\u6700\u957f&nbsp;\u5b50\u4e32<\/strong><strong>&nbsp;<\/strong>\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165: <\/strong>s = \"abcabcbb\"<\/li>\n\n\n\n<li><strong>\u8f93\u51fa: <\/strong>3 <\/li>\n\n\n\n<li><strong>\u89e3\u91ca:<\/strong> \u56e0\u4e3a\u65e0\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f <code>\"abc\"<\/code>\uff0c\u6240\u4ee5\u5176\u957f\u5ea6\u4e3a 3\u3002\u6ce8\u610f \"bca\" \u548c \"cab\" \u4e5f\u662f\u6b63\u786e\u7b54\u6848\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u53cc\u6307\u9488\uff0cleft\u6307\u5411\u5b57\u4e32\u5de6\u4fa7\uff0cright\u6307\u5411\u5b57\u4e32\u53f3\u4fa7\uff0c\u5e76\u5bf9\u5b57\u4e32\u5b57\u7b26\u8fdb\u884c\u8ba1\u6570\uff0c\u7ef4\u6301\u5b57\u4e32\u5185\u5404\u5b57\u7b26\u8ba1\u6570\u503c\u4e0d\u5927\u4e8e1\uff0c\u5e76\u8bb0\u5f55\u5408\u89c4\u5b50\u4e32\u7684\u6700\u5927\u957f\u5ea6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def lengthOfLongestSubstring(self, s: str) -&gt; int:\n        cnt=defaultdict(int)\n        left=ans=0\n        for right,c in enumerate(s):\n            cnt&#91;c] +=1\n            while cnt&#91;c]&gt;1:\n                cnt&#91;s&#91;left]] -=1\n                left +=1\n            ans=max(ans,right-left+1)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">9.<a href=\"https:\/\/leetcode.cn\/problems\/find-all-anagrams-in-a-string\/\" target=\"_blank\"  rel=\"nofollow\" >\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u6bcd\u5f02\u4f4d\u8bcd<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\u548c&nbsp;<code>p<\/code>\uff0c\u627e\u5230&nbsp;<code>s<\/code><strong>&nbsp;<\/strong>\u4e2d\u6240\u6709&nbsp;<code>p<\/code><strong>&nbsp;<\/strong>\u7684&nbsp;<strong>\u5f02\u4f4d\u8bcd&nbsp;<\/strong>\u7684\u5b50\u4e32\uff0c\u8fd4\u56de\u8fd9\u4e9b\u5b50\u4e32\u7684\u8d77\u59cb\u7d22\u5f15\u3002\u4e0d\u8003\u8651\u7b54\u6848\u8f93\u51fa\u7684\u987a\u5e8f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165: <\/strong>s = \"cbaebabacd\", p = \"abc\"<\/li>\n\n\n\n<li><strong>\u8f93\u51fa: <\/strong>[0,6]<\/li>\n\n\n\n<li><strong>\u89e3\u91ca:<\/strong><\/li>\n\n\n\n<li>\u8d77\u59cb\u7d22\u5f15\u7b49\u4e8e 0 \u7684\u5b50\u4e32\u662f \"cba\", \u5b83\u662f \"abc\" \u7684\u5f02\u4f4d\u8bcd\u3002<\/li>\n\n\n\n<li>\u8d77\u59cb\u7d22\u5f15\u7b49\u4e8e 6 \u7684\u5b50\u4e32\u662f \"bac\", \u5b83\u662f \"abc\" \u7684\u5f02\u4f4d\u8bcd\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u6ed1\u52a8\u7a97\u53e3\u52a0\u8ba1\u6570\u5339\u914d\uff0c\u5b9a\u4e49\u5b57\u5178cnt\u8bb0\u5f55p\u4e2d\u5404\u5b57\u7b26\u7684\u8ba1\u6570\uff0c\u5728\u79fb\u52a8\u7a97\u53e3\u65f6\u7ef4\u62a4\u7a97\u53e3\u5185\u5b57\u7b26\u8ba1\u6570\u503c\uff0c\u82e5\u503c\u4e3a0\u5219\u5220\u9664\u5143\u7d20\uff0c\u907f\u514d0\u4e0e\u7a7a\u503c\u65e0\u6cd5\u5339\u914d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def findAnagrams(self, s: str, p: str) -&gt; List&#91;int]:\n        w_l=len(p)\n        cnt=defaultdict(int)\n        for c in p:\n            cnt&#91;c] +=1\n        temp=defaultdict(int)\n        ans=&#91;]\n        for right, c in enumerate(s):\n            temp&#91;c] += 1  # \u53f3\u7aef\u70b9\u5b57\u6bcd\u8fdb\u5165\u7a97\u53e3\n\n            left = right - w_l + 1\n            if left &lt; 0:  # \u7a97\u53e3\u957f\u5ea6\u4e0d\u8db3 len(p)\n                continue\n\n            if cnt == temp:  # t \u548c p \u7684\u6bcf\u79cd\u5b57\u6bcd\u7684\u51fa\u73b0\u6b21\u6570\u90fd\u76f8\u540c\n                ans.append(left)  # t \u5de6\u7aef\u70b9\u4e0b\u6807\u52a0\u5165\u7b54\u6848\n\n            temp&#91;s&#91;left]] -= 1  # \u5de6\u7aef\u70b9\u5b57\u6bcd\u79bb\u5f00\u7a97\u53e3\n            if temp&#91;s&#91;left]]==0:\n                del temp&#91;s&#91;left]]\n\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">10.<a href=\"https:\/\/leetcode.cn\/problems\/subarray-sum-equals-k\/\" target=\"_blank\"  rel=\"nofollow\" >\u548c\u4e3a K \u7684\u5b50\u6570\u7ec4<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\u548c\u4e00\u4e2a\u6574\u6570&nbsp;<code>k<\/code>&nbsp;\uff0c\u8bf7\u4f60\u7edf\u8ba1\u5e76\u8fd4\u56de&nbsp;<em>\u8be5\u6570\u7ec4\u4e2d\u548c\u4e3a&nbsp;<code>k<\/code><strong>&nbsp;<\/strong>\u7684\u5b50\u6570\u7ec4\u7684\u4e2a\u6570&nbsp;<\/em>\u3002<\/p>\n\n\n\n<p>\u5b50\u6570\u7ec4\u662f\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u8fde\u7eed\u975e\u7a7a\u5e8f\u5217\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,1,1], k = 2<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>2<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u8ba1\u7b97\u524d\u5e8f\u548c\uff0c\u5b50\u6570\u7ec4\u548c\u4e3ak\uff0c\u5373s[i]-s[j]=k\uff0cs\u4e3a\u524d\u5e8f\u548c\u6570\u7ec4\uff0ci\u5fc5\u987b\u5927\u4e8ej<br>\u904d\u5386\u524d\u5e8f\u548c\u6570\u7ec4s\uff0c\u56e0\u4e3ai\u5fc5\u987b\u5927\u4e8ej\uff0c\u5c06\u5f53\u524d\u904d\u5386\u5230\u7684\u6570\u89c6\u4e3ai\uff0c\u5219j\u5728\u524d\u7f6e\u6570\u4e2d\u6311\u9009<br>\u5728\u6b64\u4f7f\u7528cnt\u5b57\u5178\u8bb0\u5f55\u4e0d\u540c\u524d\u7f6e\u548c\u51fa\u73b0\u7684\u4e2a\u6570\uff08\u53ea\u9700\u8981\u7edf\u8ba1\u4e2a\u6570\uff0c\u4e0d\u9700\u8981\u4f4d\u7f6e\uff0c\u56e0\u6b64\u65e0\u9700\u8bb0\u5f55index\uff0c\u53ea\u8bb0\u5f55\u503c\uff09\uff0cans\u6bcf\u6b21\u52a0\u4e0acnt[s[i]-k]\u5373cnt[s[j]]\uff0c\u5373\u52a0\u4e0a\u5f53\u524d\u6240\u6709s[i]-s[j]=k\u53ef\u80fd\u60c5\u51b5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def subarraySum(self, nums: List&#91;int], k: int) -&gt; int:\n        s = &#91;0]*(len(nums) + 1)\n        for i,num in enumerate(nums):\n            s&#91;i+1] = s&#91;i] + num\n        cnt = defaultdict(int)\n        ans = 0\n        for sj in s:\n            ans +=cnt&#91;sj-k]\n            cnt&#91;sj] += 1\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">11.<a href=\"https:\/\/leetcode.cn\/problems\/sliding-window-maximum\/\" target=\"_blank\"  rel=\"nofollow\" >\u6ed1\u52a8\u7a97\u53e3\u6700\u5927\u503c<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u6709\u4e00\u4e2a\u5927\u5c0f\u4e3a&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u7684\u6ed1\u52a8\u7a97\u53e3\u4ece\u6570\u7ec4\u7684\u6700\u5de6\u4fa7\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u4fa7\u3002\u4f60\u53ea\u53ef\u4ee5\u770b\u5230\u5728\u6ed1\u52a8\u7a97\u53e3\u5185\u7684&nbsp;<code>k<\/code>&nbsp;\u4e2a\u6570\u5b57\u3002\u6ed1\u52a8\u7a97\u53e3\u6bcf\u6b21\u53ea\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de&nbsp;<em>\u6ed1\u52a8\u7a97\u53e3\u4e2d\u7684\u6700\u5927\u503c&nbsp;<\/em>\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,3,-1,-3,5,3,6,7], k = 3<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[3,3,5,5,6,7]<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><br>\u6ed1\u52a8\u7a97\u53e3\u7684\u4f4d\u7f6e \u6700\u5927\u503c<br>--------------- -----<br>[1 3 -1] -3 5 3 6 7 <strong>3<\/strong><br>1 [3 -1 -3] 5 3 6 7 <strong>3<\/strong><br>1 3 [-1 -3 5] 3 6 7 <strong>5<\/strong><br>1 3 -1 [-3 5 3] 6 7 <strong>5<\/strong><br>1 3 -1 -3 [5 3 6] 7 <strong>6<\/strong><br>1 3 -1 -3 5 [3 6 7] <strong>7<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4f7f\u7528\u53cc\u7aef\u961f\u5217\u7ef4\u62a4\u6700\u5927\u503c\uff0c\u961f\u9996\u4e3a\u6700\u5927\u503c\uff0c\u7b2c\u4e8c\u4e2a\u4e3a\u961f\u9996\u5143\u7d20\u540e\u51fa\u73b0\u7684\u6700\u5927\u503c\uff0c\u5f53\u961f\u9996\u5143\u7d20\u79fb\u51fa\u7a97\u53e3\u540e\uff0c\u7b2c\u4e8c\u4e2a\u503c\u4f5c\u4e3a\u5f53\u524d\u6700\u5927\u503c<br>\u5177\u4f53\u6d41\u7a0b\u4e3a<br>1.\u4ece\u8d77\u59cb\u70b9\u5f00\u59cb\u6784\u5efa\u6ed1\u52a8\u7a97\u53e3\uff0c\u6784\u5efa\u521d\u59cb\u53cc\u7aef\u961f\u5217<br>2.\u961f\u5217\u7ef4\u62a4\u6d41\u7a0b\uff1a\u5f53\u65b0\u5143\u7d20\u8fdb\u5165\u65f6\uff0c\u82e5\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5c06\u961f\u5217\u4e2d\u6240\u6709\u5c0f\u4e8e\u65b0\u5143\u7d20\u7684\u503c\u79fb\u9664\uff0c\u518d\u5c06\u65b0\u5143\u7d20\u52a0\u5165\uff0c\u8fd9\u6837\u53ef\u4ee5\u7ef4\u6301\u5f53\u524d\u4e00\u4e2a\u6700\u5927\u503c\u79fb\u9664\u540e\uff0c\u5176\u540e\u7684\u503c\u4e3a\u5f53\u524d\u7a97\u53e3\u5185\u7684\u6700\u5927\u503c<br>3.\u79fb\u52a8\u6ed1\u52a8\u7a97\u53e3\uff0c\u5224\u65ad\u5de6\u4fa7\u79fb\u51fa\u7a97\u53e3\u7684\u503c\u662f\u5426\u4e3a\u961f\u9996\u5143\u7d20\uff0c\u82e5\u662f\u5219\u961f\u5217\u5de6\u4fa7\u5143\u7d20\u51fa\u961f\u3002\u6ed1\u52a8\u7a97\u53e3\u53f3\u4fa7\u4e00\u4e2a\u65b0\u5143\u7d20\u8fdb\u961f\uff0c\u540c\u6837\u7684\uff0c\u82e5\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5c06\u961f\u5217\u4e2d\u6240\u6709\u5c0f\u4e8e\u65b0\u5143\u7d20\u7684\u503c\u79fb\u9664\uff0c\u518d\u5c06\u65b0\u5143\u7d20\u52a0\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxSlidingWindow(self, nums: List&#91;int], k: int) -&gt; List&#91;int]:\n        l=len(nums)\n        deque = collections.deque()\n        ans = &#91;]\n        for i in range(k):\n            while deque and deque&#91;-1]&lt;nums&#91;i]:\n                deque.pop()\n            deque.append(nums&#91;i])\n        ans.append(deque&#91;0])\n        for i in range(k,l):\n            if deque&#91;0]==nums&#91;i-k]:\n                deque.popleft()\n            while deque and deque&#91;-1]&lt;nums&#91;i]:\n                deque.pop()\n            deque.append(nums&#91;i])\n            ans.append(deque&#91;0])\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">12.<a href=\"https:\/\/leetcode.cn\/problems\/minimum-window-substring\/\" target=\"_blank\"  rel=\"nofollow\" >\u6700\u5c0f\u8986\u76d6\u5b50\u4e32<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\u548c&nbsp;<code>t<\/code>\uff0c\u957f\u5ea6\u5206\u522b\u662f&nbsp;<code>m<\/code>&nbsp;\u548c&nbsp;<code>n<\/code>\uff0c\u8fd4\u56de s \u4e2d\u7684&nbsp;<strong>\u6700\u77ed\u7a97\u53e3&nbsp;\u5b50\u4e32<\/strong>\uff0c\u4f7f\u5f97\u8be5\u5b50\u4e32\u5305\u542b&nbsp;<code>t<\/code>&nbsp;\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff08<strong>\u5305\u62ec\u91cd\u590d\u5b57\u7b26<\/strong>\uff09\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u6837\u7684\u5b50\u4e32\uff0c\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<em>&nbsp;<\/em><code>\"\"<\/code>\u3002<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u7528\u4f8b\u4fdd\u8bc1\u7b54\u6848\u552f\u4e00\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>s = \"ADOBECODEBANC\", t = \"ABC\"<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>\"BANC\"<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u5c0f\u8986\u76d6\u5b50\u4e32 \"BANC\" \u5305\u542b\u6765\u81ea\u5b57\u7b26\u4e32 t \u7684 'A'\u3001'B' \u548c 'C'\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u8ba1\u6570\u5b57\u4e32t\u5b57\u7b26\u6570\uff0c\u5b9a\u7a97\u53e3\u5de6\u8d77\u70b9\u6269\u5f20\u53f3\u8fb9\u754c\u8ba1\u6570\uff0c\u76f4\u81f3\u7a97\u53e3\u5185\u8ba1\u6570\u7ed3\u679c\u5305\u542bt\u5b57\u7b26\u4e32\u8ba1\u6570\u6574\u4f53\uff0c\u518d\u5728\u7ef4\u6301\u7a97\u53e3\u5185\u8ba1\u6570\u7ed3\u679c\u5305\u542bt\u5b57\u7b26\u4e32\u8ba1\u6570\u6574\u4f53\u60c5\u51b5\u4e0b\u79fb\u52a8\u5de6\u8fb9\u754c\uff0c\u76f4\u81f3\u65e0\u6cd5\u5305\u542b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def minWindow(self, s: str, t: str) -&gt; str:\n        cnt_s = Counter()\n        cnt_t = Counter(t)\n        ans_left,ans_right=-1,len(s)\n        left=0\n        for right,c in enumerate(s):\n            cnt_s&#91;c] +=1\n            while cnt_s&gt;=cnt_t:\n                if right-left&lt;ans_right-ans_left:\n                    ans_left,ans_right=left,right\n                cnt_s&#91;s&#91;left]] -=1\n                left +=1\n        return \"\" if ans_left&lt;0 else s&#91;ans_left:ans_right+1]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">13.<a href=\"https:\/\/leetcode.cn\/problems\/maximum-subarray\/\" target=\"_blank\"  rel=\"nofollow\" >\u6700\u5927\u5b50\u6570\u7ec4\u548c<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u5177\u6709\u6700\u5927\u548c\u7684\u8fde\u7eed\u5b50\u6570\u7ec4\uff08\u5b50\u6570\u7ec4\u6700\u5c11\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u8fd4\u56de\u5176\u6700\u5927\u548c\u3002<\/p>\n\n\n\n<p><strong>\u5b50\u6570\u7ec4<\/strong>\u662f\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u8fde\u7eed\u90e8\u5206\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [-2,1,-3,4,-1,2,1,-5,4]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>6<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u8fde\u7eed\u5b50\u6570\u7ec4&nbsp;[4,-1,2,1] \u7684\u548c\u6700\u5927\uff0c\u4e3a&nbsp;6 \u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4e24\u4e2a\u601d\u8def<br>\u601d\u8def1\uff1a\u904d\u5386\u8fc7\u7a0b\u4e2d\u8ba1\u7b97\u524d\u7f00\u548c\uff0c\u5e76\u8bb0\u5f55\u524d\u7f00\u548c\u7684\u6700\u5c0f\u503c\uff0c\u5f53\u524d\u524d\u7f00\u548c-\u524d\u7f00\u548c\u7684\u6700\u5c0f\u503c\u5f97\u5230\u7684\u6700\u5927\u503c\u5373\u4e3a\u6700\u5927\u5b50\u6570\u7ec4\u548c<br>\u601d\u8def2\uff1a\u7ef4\u62a4\u524d\u7f6e\u6700\u5927\u5b50\u6570\u7ec4\u548c\uff0c\u53d6max(pre_max,0)<\/p>\n\n\n\n<p>\u601d\u8def1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxSubArray(self, nums: List&#91;int]) -&gt; int:\n        ans = -inf\n        min_pre_sum = pre_sum = 0\n        for x in nums:\n            pre_sum += x  # \u5f53\u524d\u7684\u524d\u7f00\u548c\n            ans = max(ans, pre_sum - min_pre_sum)  # \u51cf\u53bb\u524d\u7f00\u548c\u7684\u6700\u5c0f\u503c\n            min_pre_sum = min(min_pre_sum, pre_sum)  # \u7ef4\u62a4\u524d\u7f00\u548c\u7684\u6700\u5c0f\u503c\n        return ans<\/code><\/pre>\n\n\n\n<p>\u601d\u8def2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxSubArray(self, nums: List&#91;int]) -&gt; int:\n        ans=-inf\n        pre_max=0\n        for x in nums:\n            pre_max =max(pre_max,0)+x\n            ans=max(ans,pre_max)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">14.<a href=\"https:\/\/leetcode.cn\/problems\/merge-intervals\/\" target=\"_blank\"  rel=\"nofollow\" >\u5408\u5e76\u533a\u95f4<\/a><\/h2>\n\n\n\n<p>\u4ee5\u6570\u7ec4&nbsp;<code>intervals<\/code>&nbsp;\u8868\u793a\u82e5\u5e72\u4e2a\u533a\u95f4\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u5355\u4e2a\u533a\u95f4\u4e3a&nbsp;<code>intervals[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>]<\/code>&nbsp;\u3002\u8bf7\u4f60\u5408\u5e76\u6240\u6709\u91cd\u53e0\u7684\u533a\u95f4\uff0c\u5e76\u8fd4\u56de&nbsp;<em>\u4e00\u4e2a\u4e0d\u91cd\u53e0\u7684\u533a\u95f4\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u9700\u6070\u597d\u8986\u76d6\u8f93\u5165\u4e2d\u7684\u6240\u6709\u533a\u95f4<\/em>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>intervals = [[1,3],[2,6],[8,10],[15,18]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[1,6],[8,10],[15,18]]<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u533a\u95f4 [1,3] \u548c [2,6] \u91cd\u53e0, \u5c06\u5b83\u4eec\u5408\u5e76\u4e3a [1,6].<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5c06\u6570\u7ec4\u5185\u533a\u95f4\u6309\u5de6\u8fb9\u754c\u6392\u5e8f\uff0c\u904d\u5386\u65f6\u5219\u6ee1\u8db3\u65b0\u7684\u533a\u95f4\u5de6\u8fb9\u754c\u4e00\u5b9a\u66f4\u5927\uff0c\u82e5\u5de6\u8fb9\u754c\u5c0f\u4e8e\u6700\u65b0\u5408\u5e76\u533a\u95f4\u7684\u53f3\u8fb9\u754c\uff0c\u5219\u53ef\u4ee5\u8fdb\u884c\u5408\u5e76\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def merge(self, intervals: List&#91;List&#91;int]]) -&gt; List&#91;List&#91;int]]:\n        intervals.sort(key=lambda p:p&#91;0])\n        ans=&#91;]\n        for p in intervals:\n            if ans and p&#91;0]&lt;=ans&#91;-1]&#91;1]:\n                ans&#91;-1]&#91;1]=max(ans&#91;-1]&#91;1],p&#91;1])\n            else:\n                ans.append(p)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">15.<a href=\"https:\/\/leetcode.cn\/problems\/rotate-array\/\" target=\"_blank\"  rel=\"nofollow\" >&nbsp;\u8f6e\u8f6c\u6570\u7ec4<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5411\u53f3\u8f6e\u8f6c&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u4e2a\u4f4d\u7f6e\uff0c\u5176\u4e2d&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u662f\u975e\u8d1f\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165:<\/strong> nums = [1,2,3,4,5,6,7], k = 3<\/li>\n\n\n\n<li><strong>\u8f93\u51fa:<\/strong> <code>[5,6,7,1,2,3,4]<\/code><\/li>\n\n\n\n<li><strong>\u89e3\u91ca:<\/strong><\/li>\n\n\n\n<li>\u5411\u53f3\u8f6e\u8f6c 1 \u6b65: <code>[7,1,2,3,4,5,6]<\/code><\/li>\n\n\n\n<li>\u5411\u53f3\u8f6e\u8f6c 2 \u6b65: <code>[6,7,1,2,3,4,5]<\/code><\/li>\n\n\n\n<li>\u5411\u53f3\u8f6e\u8f6c 3 \u6b65: <code>[5,6,7,1,2,3,4]<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u8f6e\u8f6ck\u4e2a\u4f4d\u7f6e\uff0c\u5373\u540ek\u4e2a\u6570\u88ab\u63d0\u524d\u5230\u4e86\u6570\u7ec4\u9996\u4f4d\u7f6e\uff0c\u4e14\u53cd\u8f6c\u4e24\u5c42\u4e3a\u6b63\u5e8f\uff0c\u56e0\u6b64\u5148\u6574\u4f53\u53cd\u8f6c\uff0c\u518d\u53cd\u8f6c\u524dk\u4e2a\u6570\uff0c\u518d\u5c06\u540e\u7eed\u6570\u53cd\u8f6c\u56de\u6b63\u5e8f\uff0c\u5373\u5b9e\u73b0\u4e86\u5c06\u540ek\u4e2a\u6570\u88ab\u63d0\u524d\u5230\u4e86\u6570\u7ec4\u9996\u4f4d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def rotate(self, nums: List&#91;int], k: int) -&gt; None:\n        \"\"\"\n        Do not return anything, modify nums in-place instead.\n        \"\"\"\n        def reverse(i:int,j:int)-&gt;None:\n            while i&lt;j:\n                nums&#91;i],nums&#91;j]=nums&#91;j],nums&#91;i]\n                i+=1\n                j-=1\n\n        l=len(nums)\n        k=k%l\n        reverse(0,l-1)\n        reverse(0,k-1)\n        reverse(k,l-1)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">16.<a href=\"https:\/\/leetcode.cn\/problems\/product-of-array-except-self\/\" target=\"_blank\"  rel=\"nofollow\" >&nbsp;\u9664\u4e86\u81ea\u8eab\u4ee5\u5916\u6570\u7ec4\u7684\u4e58\u79ef<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u8fd4\u56de \u6570\u7ec4&nbsp;<code>answer<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>answer[i]<\/code>&nbsp;\u7b49\u4e8e&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u9664\u4e86&nbsp;<code>nums[i]<\/code>&nbsp;\u4e4b\u5916\u5176\u4f59\u5404\u5143\u7d20\u7684\u4e58\u79ef&nbsp;\u3002<\/p>\n\n\n\n<p>\u9898\u76ee\u6570\u636e&nbsp;<strong>\u4fdd\u8bc1<\/strong>&nbsp;\u6570\u7ec4&nbsp;<code>nums<\/code>\u4e4b\u4e2d\u4efb\u610f\u5143\u7d20\u7684\u5168\u90e8\u524d\u7f00\u5143\u7d20\u548c\u540e\u7f00\u7684\u4e58\u79ef\u90fd\u5728&nbsp;&nbsp;<strong>32 \u4f4d<\/strong>&nbsp;\u6574\u6570\u8303\u56f4\u5185\u3002<\/p>\n\n\n\n<p>\u8bf7&nbsp;<strong>\u4e0d\u8981\u4f7f\u7528\u9664\u6cd5\uff0c<\/strong>\u4e14\u5728&nbsp;<code>O(n)<\/code>&nbsp;\u65f6\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210\u6b64\u9898\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165:<\/strong> nums = <code>[1,2,3,4]<\/code><\/li>\n\n\n\n<li><strong>\u8f93\u51fa:<\/strong> <code>[24,12,8,6]<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u8ba1\u7b97\u524d\u5e8f\u4e58\u79ef\uff0c\u540e\u5e8f\u4e58\u79ef\uff0c\u9664\u81ea\u8eab\u5916\u7684\u4e58\u79ef\u4e3a\u5de6\u4fa7\u524d\u5e8f\u4e58\u79ef\u4e58\u53f3\u4fa7\u540e\u7eed\u4e58\u79ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def productExceptSelf(self, nums: List&#91;int]) -&gt; List&#91;int]:\n        n=len(nums)\n        pre=&#91;1]*n\n        suf=&#91;1]*n\n        for i in range(1, n):\n            pre&#91;i] = pre&#91;i - 1] * nums&#91;i - 1]\n\n        for i in range(n - 2, -1, -1):\n            suf&#91;i] = suf&#91;i + 1] * nums&#91;i + 1]\n\n        res=&#91;]\n        for p,s in zip(pre,suf):\n            res.append(p*s)\n        return res<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">17.<a href=\"https:\/\/leetcode.cn\/problems\/first-missing-positive\/\" target=\"_blank\"  rel=\"nofollow\" >&nbsp;\u7f3a\u5931\u7684\u7b2c\u4e00\u4e2a\u6b63\u6570<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u672a\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u5176\u4e2d\u6ca1\u6709\u51fa\u73b0\u7684\u6700\u5c0f\u7684\u6b63\u6574\u6570\u3002\u8bf7\u4f60\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(n)<\/code>&nbsp;\u5e76\u4e14\u53ea\u4f7f\u7528\u5e38\u6570\u7ea7\u522b\u989d\u5916\u7a7a\u95f4\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,2,0]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>3<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u8303\u56f4 [1,2] \u4e2d\u7684\u6570\u5b57\u90fd\u5728\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u7f3a\u5931\u7684\u7b2c\u4e00\u4e2a\u6b63\u6570\uff0c\u6b63\u6570\u4ece1\u5f00\u59cb\uff0c\u53ef\u4e0e\u6570\u7ec4index\u5bf9\u5e94\uff0c\u6309index=value\u91cd\u6392\uff0c\u627e\u5230index!=value\uff0c\u5373\u627e\u5230\u7ed3\u679c\u3002\u82e5\u91cd\u6392\u540e\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5747\u6ee1\u8db3\u6761\u4ef6\uff0c\u5219\u6700\u5c0f\u6b63\u6574\u6570\u4e3alen(nums)+1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def firstMissingPositive(self, nums: List&#91;int]) -&gt; int:\n        for i in range(len(nums)):\n            # \u6ce8\u610f\u6b64\u5904\u4e0d\u80fd\u7528if\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u90e8\u5206\u5143\u7d20\u88ab\u8df3\u8fc7\uff0c\u6ca1\u6709\u88ab\u653e\u7f6e\u5230\u6b63\u786e\u4f4d\u7f6e\n            while 1 &lt;= nums&#91;i] &lt;= len(nums) and nums&#91;nums&#91;i] - 1] != nums&#91;i]:\n                # \u90a3\u4e48\u5c31\u4ea4\u6362 nums&#91;i] \u548c nums&#91;j]\uff0c\u5176\u4e2d j \u662f i \u7684\u5b66\u53f7\n                j = nums&#91;i] - 1  # \u51cf\u4e00\u662f\u56e0\u4e3a\u6570\u7ec4\u4e0b\u6807\u4ece 0 \u5f00\u59cb\n                nums&#91;i], nums&#91;j] = nums&#91;j], nums&#91;i]\n        for i,n in enumerate(nums):\n            if n!=i+1:\n                return i+1\n        return len(nums)+1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">18.<a href=\"https:\/\/leetcode.cn\/problems\/set-matrix-zeroes\/\" target=\"_blank\"  rel=\"nofollow\" >\u77e9\u9635\u7f6e\u96f6<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a&nbsp;<code><em>m<\/em> x <em>n<\/em><\/code>&nbsp;\u7684\u77e9\u9635\uff0c\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u4e3a&nbsp;<strong>0&nbsp;<\/strong>\uff0c\u5219\u5c06\u5176\u6240\u5728\u884c\u548c\u5217\u7684\u6240\u6709\u5143\u7d20\u90fd\u8bbe\u4e3a&nbsp;<strong>0<\/strong>&nbsp;\u3002\u8bf7\u4f7f\u7528&nbsp;<strong><a href=\"http:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u539f\u5730<\/a><\/strong>&nbsp;\u7b97\u6cd5<strong>\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/17\/mat1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[1,0,1],[0,0,0],[1,0,1]]<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/17\/mat2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4f7f\u7528\u4e24\u4e2aset()\u5206\u522b\u8bb0\u5f55\u4e0b0\u70b9\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\uff0c\u518d\u6839\u636e\u8bb0\u5f55\u8fdb\u884c\u7f6e\u96f6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def setZeroes(self, matrix: List&#91;List&#91;int]]) -&gt; None:\n        \"\"\"\n        Do not return anything, modify matrix in-place instead.\n        \"\"\"\n        # \u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\n        row_count = len(matrix)\n        col_count = len(matrix&#91;0]) if row_count &gt; 0 else 0\n        \n        # \u7528\u96c6\u5408\u8bb0\u5f55\u9700\u8981\u7f6e\u96f6\u7684\u884c\u548c\u5217\n        zero_rows = set()\n        zero_cols = set()\n        \n        # \u7b2c\u4e00\u6b65\uff1a\u904d\u5386\u77e9\u9635\uff0c\u6807\u8bb0\u6240\u67090\u6240\u5728\u7684\u884c\u548c\u5217\n        for i in range(row_count):\n            for j in range(col_count):\n                if matrix&#91;i]&#91;j] == 0:\n                    zero_rows.add(i)  # \u8bb0\u5f550\u6240\u5728\u7684\u884c\u7d22\u5f15i\n                    zero_cols.add(j)  # \u8bb0\u5f550\u6240\u5728\u7684\u5217\u7d22\u5f15j\n        \n        # \u7b2c\u4e8c\u6b65\uff1a\u5c06\u6807\u8bb0\u7684\u884c\u5168\u90e8\u7f6e\u96f6\n        for row_idx in zero_rows:\n            for col_idx in range(col_count):\n                matrix&#91;row_idx]&#91;col_idx] = 0\n        \n        # \u7b2c\u4e09\u6b65\uff1a\u5c06\u6807\u8bb0\u7684\u5217\u5168\u90e8\u7f6e\u96f6\n        for col_idx in zero_cols:\n            for row_idx in range(row_count):\n                matrix&#91;row_idx]&#91;col_idx] = 0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">19.<a href=\"https:\/\/leetcode.cn\/problems\/spiral-matrix\/\" target=\"_blank\"  rel=\"nofollow\" >\u87ba\u65cb\u77e9\u9635<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;<code>m<\/code>&nbsp;\u884c&nbsp;<code>n<\/code>&nbsp;\u5217\u7684\u77e9\u9635&nbsp;<code>matrix<\/code>&nbsp;\uff0c\u8bf7\u6309\u7167&nbsp;<strong>\u987a\u65f6\u9488\u87ba\u65cb\u987a\u5e8f<\/strong>&nbsp;\uff0c\u8fd4\u56de\u77e9\u9635\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/13\/spiral1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,6,9,8,7,4,5]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u6a21\u62df\u904d\u5386\u8fc7\u7a0b\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def spiralOrder(self, matrix: List&#91;List&#91;int]]) -&gt; List&#91;int]:\n        \"\"\"\n        \u87ba\u65cb\u987a\u5e8f\u904d\u5386\u4e8c\u7ef4\u77e9\u9635\uff0c\u8fd4\u56de\u904d\u5386\u7ed3\u679c\u5217\u8868\n        :param matrix: \u5f85\u904d\u5386\u7684\u4e8c\u7ef4\u77e9\u9635\n        :return: \u6309\u87ba\u65cb\u987a\u5e8f\u6392\u5217\u7684\u5143\u7d20\u5217\u8868\n        \"\"\"\n        # \u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\n        row_cnt = len(matrix)\n        # \u83b7\u53d6\u77e9\u9635\u7684\u5217\u6570\uff08\u5904\u7406\u7a7a\u77e9\u9635\u907f\u514d\u7d22\u5f15\u8d8a\u754c\uff09\n        col_cnt = len(matrix&#91;0]) if row_cnt &gt; 0 else 0\n        \n        # \u5b58\u50a8\u6700\u7ec8\u7684\u87ba\u65cb\u904d\u5386\u7ed3\u679c\n        res = &#91;]\n        \n        # \u5b9a\u4e49\u56db\u4e2a\u8fb9\u754c\uff1a\u5de6\u3001\u53f3\u3001\u4e0a\u3001\u4e0b\n        left = 0          # \u5de6\u8fb9\u754c\uff08\u521d\u59cb\u4e3a\u7b2c0\u5217\uff09\n        right = col_cnt - 1  # \u53f3\u8fb9\u754c\uff08\u521d\u59cb\u4e3a\u6700\u540e\u4e00\u5217\uff09\n        top = 0           # \u4e0a\u8fb9\u754c\uff08\u521d\u59cb\u4e3a\u7b2c0\u884c\uff09\n        bottom = row_cnt - 1 # \u4e0b\u8fb9\u754c\uff08\u521d\u59cb\u4e3a\u6700\u540e\u4e00\u884c\uff09\n        \n        # \u5faa\u73af\u6761\u4ef6\uff1a\u5de6\u8fb9\u754c\u4e0d\u8d85\u8fc7\u53f3\u8fb9\u754c\uff0c\u4e14\u4e0a\u8fb9\u754c\u4e0d\u8d85\u8fc7\u4e0b\u8fb9\u754c\n        while left &lt;= right and top &lt;= bottom:\n            # \u7b2c\u4e00\u6b65\uff1a\u4ece\u5de6\u5230\u53f3\u904d\u5386\u4e0a\u8fb9\u754c\u6240\u5728\u7684\u884c\uff08\u9876\u884c\uff09\n            for column in range(left, right + 1):\n                res.append(matrix&#91;top]&#91;column])\n            # \u7b2c\u4e8c\u6b65\uff1a\u4ece\u4e0a\u5230\u4e0b\u904d\u5386\u53f3\u8fb9\u754c\u6240\u5728\u7684\u5217\uff08\u53f3\u5217\uff09\uff0c\u6ce8\u610f\u8df3\u8fc7\u5df2\u904d\u5386\u7684\u9876\u884c\u5143\u7d20\n            for row in range(top + 1, bottom + 1):\n                res.append(matrix&#91;row]&#91;right])\n            \n            # \u5173\u952e\u5224\u65ad\uff1a\u53ea\u6709\u5f53\u5de6&lt;\u53f3\u4e14\u4e0a&lt;\u4e0b\u65f6\uff0c\u624d\u9700\u8981\u904d\u5386\u4e0b\u9762\u548c\u5de6\u9762\uff08\u907f\u514d\u5355\u884c\/\u5355\u5217\u91cd\u590d\u904d\u5386\uff09\n            if left &lt; right and top &lt; bottom:\n                # \u7b2c\u4e09\u6b65\uff1a\u4ece\u53f3\u5230\u5de6\u904d\u5386\u4e0b\u8fb9\u754c\u6240\u5728\u7684\u884c\uff08\u5e95\u884c\uff09\uff0c\u6ce8\u610f\u8df3\u8fc7\u5df2\u904d\u5386\u7684\u53f3\u5217\u5143\u7d20\n                for column in range(right - 1, left, -1):\n                    res.append(matrix&#91;bottom]&#91;column])\n                # \u7b2c\u56db\u6b65\uff1a\u4ece\u4e0b\u5230\u4e0a\u904d\u5386\u5de6\u8fb9\u754c\u6240\u5728\u7684\u5217\uff08\u5de6\u5217\uff09\uff0c\u6ce8\u610f\u8df3\u8fc7\u5df2\u904d\u5386\u7684\u5e95\u884c\u548c\u9876\u884c\u5143\u7d20\n                for row in range(bottom, top, -1):\n                    res.append(matrix&#91;row]&#91;left])\n            \n            # \u6536\u7f29\u8fb9\u754c\uff1a\u5de6\u8fb9\u754c\u53f3\u79fb\u3001\u53f3\u8fb9\u754c\u5de6\u79fb\u3001\u4e0a\u8fb9\u754c\u4e0b\u79fb\u3001\u4e0b\u8fb9\u754c\u4e0a\u79fb\uff0c\u8fdb\u5165\u5185\u5c42\u5faa\u73af\n            left += 1\n            right -= 1\n            top += 1\n            bottom -= 1\n        \n        # \u8fd4\u56de\u87ba\u65cb\u904d\u5386\u7ed3\u679c\n        return res<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">20.<a href=\"https:\/\/leetcode.cn\/problems\/rotate-image\/\" target=\"_blank\"  rel=\"nofollow\" >\u65cb\u8f6c\u56fe\u50cf<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a&nbsp;<em>n&nbsp;<\/em>\u00d7&nbsp;<em>n<\/em>&nbsp;\u7684\u4e8c\u7ef4\u77e9\u9635&nbsp;<code>matrix<\/code>&nbsp;\u8868\u793a\u4e00\u4e2a\u56fe\u50cf\u3002\u8bf7\u4f60\u5c06\u56fe\u50cf\u987a\u65f6\u9488\u65cb\u8f6c 90 \u5ea6\u3002<\/p>\n\n\n\n<p>\u4f60\u5fc5\u987b\u5728<strong><a href=\"https:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >&nbsp;\u539f\u5730<\/a><\/strong>&nbsp;\u65cb\u8f6c\u56fe\u50cf\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u76f4\u63a5\u4fee\u6539\u8f93\u5165\u7684\u4e8c\u7ef4\u77e9\u9635\u3002<strong>\u8bf7\u4e0d\u8981&nbsp;<\/strong>\u4f7f\u7528\u53e6\u4e00\u4e2a\u77e9\u9635\u6765\u65cb\u8f6c\u56fe\u50cf\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/28\/mat1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[7,4,1],[8,5,2],[9,6,3]]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u7ffb\u8f6c\uff0c\u5728\u6cbf\u4e2d\u8f74\u7ffb\u8f6c\uff0c\u5373\u53ef\u5f97\u5230\u987a\u65f6\u9488\u65cb\u8f6c90\u5ea6\u7684\u56fe\u50cf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def rotate(self, matrix: List&#91;List&#91;int]]) -&gt; None:\n        \"\"\"\n        Do not return anything, modify matrix in-place instead.\n        \"\"\"\n        n=len(matrix)\n        for i in range(n):\n            for j in range(i):\n                matrix&#91;i]&#91;j],matrix&#91;j]&#91;i]=matrix&#91;j]&#91;i],matrix&#91;i]&#91;j]\n        for row in matrix:\n            row.reverse()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">21.<a href=\"https:\/\/leetcode.cn\/problems\/search-a-2d-matrix-ii\/\" target=\"_blank\"  rel=\"nofollow\" >\u641c\u7d22\u4e8c\u7ef4\u77e9\u9635 II<\/a><\/h2>\n\n\n\n<p>\u7f16\u5199\u4e00\u4e2a\u9ad8\u6548\u7684\u7b97\u6cd5\u6765\u641c\u7d22&nbsp;<code><em>m<\/em>&nbsp;x&nbsp;<em>n<\/em><\/code>&nbsp;\u77e9\u9635&nbsp;<code>matrix<\/code>&nbsp;\u4e2d\u7684\u4e00\u4e2a\u76ee\u6807\u503c&nbsp;<code>target<\/code>&nbsp;\u3002\u8be5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u884c\u7684\u5143\u7d20\u4ece\u5de6\u5230\u53f3\u5347\u5e8f\u6392\u5217\u3002<\/li>\n\n\n\n<li>\u6bcf\u5217\u7684\u5143\u7d20\u4ece\u4e0a\u5230\u4e0b\u5347\u5e8f\u6392\u5217\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2020\/11\/25\/searchgrid2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2020\/11\/25\/searchgrid.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>false<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u6bcf\u884c\u7684\u5143\u7d20\u4ece\u5de6\u5230\u53f3\u5347\u5e8f\u6392\u5217\uff0c\u6bcf\u5217\u7684\u5143\u7d20\u4ece\u4e0a\u5230\u4e0b\u5347\u5e8f\u6392\u5217\u3002\u56e0\u6b64\u53ef\u4ee5\u6839\u636e\u6bcf\u884c\u5de6\u53f3\u6781\u503c\u5224\u65adtarget\u662f\u5426\u6709\u5728\u672c\u884c\u7684\u53ef\u80fd\uff0c\u82e5\u6709\u5219\u67e5\u627e\uff0c\u6ca1\u6709\u5219\u8df3\u8fc7\u6216\u6839\u636e\u60c5\u51b5\u8fd4\u56deFalse\u3002\u67e5\u627e\u65f6\u53ef\u91c7\u7528\u4e8c\u5206\u67e5\u627e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def searchMatrix(self, matrix: List&#91;List&#91;int]], target: int) -&gt; bool:\n        row = len(matrix)\n        col = len(matrix&#91;0])\n        if target&lt;matrix&#91;0]&#91;0] or target&gt;matrix&#91;row-1]&#91;col-1]:\n            return False\n        p_row = 0\n        while p_row&lt;row:\n            if target&lt;matrix&#91;p_row]&#91;0]:\n                return False\n            elif target&gt;matrix&#91;p_row]&#91;-1]:\n                p_row +=1\n                continue\n            else:\n                left,right=0,col-1\n                while left&lt;=right:\n                    mid=(left+right)\/\/2\n                    if target==matrix&#91;p_row]&#91;mid]:\n                        return True\n                    elif target&gt;matrix&#91;p_row]&#91;mid]:\n                        left=mid+1\n                    else:\n                        right=mid -1\n                p_row +=1\n        return False<\/code><\/pre>\n\n\n\n<p>\u5de7\u5999\u89e3\u6cd5\uff1a(\u7075\u8336\u5c71\u827e\u5e9c)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def searchMatrix(self, matrix: List&#91;List&#91;int]], target: int) -&gt; bool:\n        m, n = len(matrix), len(matrix&#91;0])\n        i, j = 0, n - 1  # \u4ece\u53f3\u4e0a\u89d2\u5f00\u59cb\n        while i &lt; m and j &gt;= 0:  # \u8fd8\u6709\u5269\u4f59\u5143\u7d20\n            if matrix&#91;i]&#91;j] == target:\n                return True  # \u627e\u5230 target\n            if matrix&#91;i]&#91;j] &lt; target:\n                i += 1  # \u8fd9\u4e00\u884c\u5269\u4f59\u5143\u7d20\u5168\u90e8\u5c0f\u4e8e target\uff0c\u6392\u9664\n            else:\n                j -= 1  # \u8fd9\u4e00\u5217\u5269\u4f59\u5143\u7d20\u5168\u90e8\u5927\u4e8e target\uff0c\u6392\u9664\n        return False<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">22.<a href=\"https:\/\/leetcode.cn\/problems\/intersection-of-two-linked-lists\/\" target=\"_blank\"  rel=\"nofollow\" >\u76f8\u4ea4\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e24\u4e2a\u5355\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>headA<\/code>&nbsp;\u548c&nbsp;<code>headB<\/code>&nbsp;\uff0c\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u4e24\u4e2a\u5355\u94fe\u8868\u76f8\u4ea4\u7684\u8d77\u59cb\u8282\u70b9\u3002\u5982\u679c\u4e24\u4e2a\u94fe\u8868\u4e0d\u5b58\u5728\u76f8\u4ea4\u8282\u70b9\uff0c\u8fd4\u56de&nbsp;<code>null<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u56fe\u793a\u4e24\u4e2a\u94fe\u8868\u5728\u8282\u70b9&nbsp;<code>c1<\/code>&nbsp;\u5f00\u59cb\u76f8\u4ea4<strong>\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2018\/12\/14\/160_statement.png\" alt=\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0\" title=\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" ><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2018\/12\/14\/160_statement.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>\u9898\u76ee\u6570\u636e&nbsp;<strong>\u4fdd\u8bc1<\/strong>&nbsp;\u6574\u4e2a\u94fe\u5f0f\u7ed3\u6784\u4e2d\u4e0d\u5b58\u5728\u73af\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff0c\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u540e\uff0c\u94fe\u8868\u5fc5\u987b&nbsp;<strong>\u4fdd\u6301\u5176\u539f\u59cb\u7ed3\u6784<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u81ea\u5b9a\u4e49\u8bc4\u6d4b\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8bc4\u6d4b\u7cfb\u7edf<\/strong>&nbsp;\u7684\u8f93\u5165\u5982\u4e0b\uff08\u4f60\u8bbe\u8ba1\u7684\u7a0b\u5e8f&nbsp;<strong>\u4e0d\u9002\u7528<\/strong>&nbsp;\u6b64\u8f93\u5165\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>intersectVal<\/code>&nbsp;- \u76f8\u4ea4\u7684\u8d77\u59cb\u8282\u70b9\u7684\u503c\u3002\u5982\u679c\u4e0d\u5b58\u5728\u76f8\u4ea4\u8282\u70b9\uff0c\u8fd9\u4e00\u503c\u4e3a&nbsp;<code>0<\/code><\/li>\n\n\n\n<li><code>listA<\/code>&nbsp;- \u7b2c\u4e00\u4e2a\u94fe\u8868<\/li>\n\n\n\n<li><code>listB<\/code>&nbsp;- \u7b2c\u4e8c\u4e2a\u94fe\u8868<\/li>\n\n\n\n<li><code>skipA<\/code>&nbsp;- \u5728&nbsp;<code>listA<\/code>&nbsp;\u4e2d\uff08\u4ece\u5934\u8282\u70b9\u5f00\u59cb\uff09\u8df3\u5230\u4ea4\u53c9\u8282\u70b9\u7684\u8282\u70b9\u6570<\/li>\n\n\n\n<li><code>skipB<\/code>&nbsp;- \u5728&nbsp;<code>listB<\/code>&nbsp;\u4e2d\uff08\u4ece\u5934\u8282\u70b9\u5f00\u59cb\uff09\u8df3\u5230\u4ea4\u53c9\u8282\u70b9\u7684\u8282\u70b9\u6570<\/li>\n<\/ul>\n\n\n\n<p>\u8bc4\u6d4b\u7cfb\u7edf\u5c06\u6839\u636e\u8fd9\u4e9b\u8f93\u5165\u521b\u5efa\u94fe\u5f0f\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u5c06\u4e24\u4e2a\u5934\u8282\u70b9&nbsp;<code>headA<\/code>&nbsp;\u548c&nbsp;<code>headB<\/code>&nbsp;\u4f20\u9012\u7ed9\u4f60\u7684\u7a0b\u5e8f\u3002\u5982\u679c\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8fd4\u56de\u76f8\u4ea4\u8282\u70b9\uff0c\u90a3\u4e48\u4f60\u7684\u89e3\u51b3\u65b9\u6848\u5c06\u88ab&nbsp;<strong>\u89c6\u4f5c\u6b63\u786e\u7b54\u6848<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/13\/160_example_1.png\" alt=\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0\" title=\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" ><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/05\/160_example_1_1.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>Intersected at '8'<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong>\u76f8\u4ea4\u8282\u70b9\u7684\u503c\u4e3a 8 \uff08\u6ce8\u610f\uff0c\u5982\u679c\u4e24\u4e2a\u94fe\u8868\u76f8\u4ea4\u5219\u4e0d\u80fd\u4e3a 0\uff09\u3002<\/p>\n\n\n\n<p>\u4ece\u5404\u81ea\u7684\u8868\u5934\u5f00\u59cb\u7b97\u8d77\uff0c\u94fe\u8868 A \u4e3a [4,1,8,4,5]\uff0c\u94fe\u8868 B \u4e3a [5,6,1,8,4,5]\u3002<\/p>\n\n\n\n<p>\u5728 A \u4e2d\uff0c\u76f8\u4ea4\u8282\u70b9\u524d\u6709 2 \u4e2a\u8282\u70b9\uff1b\u5728 B \u4e2d\uff0c\u76f8\u4ea4\u8282\u70b9\u524d\u6709 3 \u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u2014 \u8bf7\u6ce8\u610f\u76f8\u4ea4\u8282\u70b9\u7684\u503c\u4e0d\u4e3a 1\uff0c\u56e0\u4e3a\u5728\u94fe\u8868 A \u548c\u94fe\u8868 B \u4e4b\u4e2d\u503c\u4e3a 1 \u7684\u8282\u70b9 (A \u4e2d\u7b2c\u4e8c\u4e2a\u8282\u70b9\u548c B \u4e2d\u7b2c\u4e09\u4e2a\u8282\u70b9) \u662f\u4e0d\u540c\u7684\u8282\u70b9\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u6307\u5411\u4e24\u4e2a\u4e0d\u540c\u7684\u4f4d\u7f6e\uff0c\u800c\u94fe\u8868 A \u548c\u94fe\u8868 B \u4e2d\u503c\u4e3a 8 \u7684\u8282\u70b9 (A \u4e2d\u7b2c\u4e09\u4e2a\u8282\u70b9\uff0cB \u4e2d\u7b2c\u56db\u4e2a\u8282\u70b9) \u5728\u5185\u5b58\u4e2d\u6307\u5411\u76f8\u540c\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4ece\u4e24\u4e2a\u8d77\u70b9\u51fa\u53d1\u904d\u5386\u4e24\u6761\u94fe\u8868\uff0c\u4e24\u6761\u8def\u7ebf\u4e4b\u548c\u5747\u4e3a\u4e24\u6761\u94fe\u8868\u957f\u5ea6\u4e4b\u548c\u3002\u82e5\u6709\u76f8\u4ea4\uff0c\u5219\u4f1a\u5728\u4ea4\u70b9\u76f8\u9047<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -&gt; Optional&#91;ListNode]:\n        p1=headA\n        p2=headB\n        cnt=0\n        while cnt&lt;2:\n            if p1==p2:\n                return p1\n            if p1.next:\n                p1=p1.next\n            else :\n                p1=headB\n                cnt+=1\n            if p2.next:\n                p2=p2.next\n            else:\n                p2=headA\n        return None<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">23.<a href=\"https:\/\/leetcode.cn\/problems\/reverse-linked-list\/\" target=\"_blank\"  rel=\"nofollow\" >&nbsp;\u53cd\u8f6c\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u5355\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>&nbsp;\uff0c\u8bf7\u4f60\u53cd\u8f6c\u94fe\u8868\uff0c\u5e76\u8fd4\u56de\u53cd\u8f6c\u540e\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/19\/rev1ex1.jpg\"><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[5,4,3,2,1]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u539f\u5730\u53cd\u8f6c\u5373\u53ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def reverseList(self, head: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        p=head\n        last=None\n        while p:\n            temp=p.next\n            p.next=last\n            last=p\n            p=temp\n        return last<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">24.<a href=\"https:\/\/leetcode.cn\/problems\/palindrome-linked-list\/\" target=\"_blank\"  rel=\"nofollow\" >\u56de\u6587\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u5355\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>&nbsp;\uff0c\u8bf7\u4f60\u5224\u65ad\u8be5\u94fe\u8868\u662f\u5426\u4e3a\u56de\u6587\u94fe\u8868\u3002\u5982\u679c\u662f\uff0c\u8fd4\u56de&nbsp;<code>true<\/code>&nbsp;\uff1b\u5426\u5219\uff0c\u8fd4\u56de&nbsp;<code>false<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/03\/pal1linked-list.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,2,1]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u8f6c\u6362\u6210\u6570\u7ec4\u518d\u53cd\u8f6c\u5bf9\u6bd4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def isPalindrome(self, head: Optional&#91;ListNode]) -&gt; bool:\n        temp=&#91;]\n        p=head\n        while p:\n            temp.append(p.val)\n            p=p.next\n        if temp==temp&#91;::-1]:\n            return True\n        else:\n            return False<\/code><\/pre>\n\n\n\n<p>\u5feb\u6162\u6307\u9488\u627e\u5230\u94fe\u8868\u4e2d\u70b9\uff0c\u53cd\u8f6c\u540e\u534a\u90e8\u5206\u94fe\u8868\uff0c\u4ece\u4e24\u5934\u5411\u4e2d\u95f4\u8d8b\u8fd1\u5bf9\u6bd4\uff08\u7075\u8336\u5c71\u827e\u5e9c\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    # 876. \u94fe\u8868\u7684\u4e2d\u95f4\u7ed3\u70b9\n    def middleNode(self, head: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        slow = fast = head\n        while fast and fast.next:\n            slow = slow.next\n            fast = fast.next.next\n        return slow\n\n    # 206. \u53cd\u8f6c\u94fe\u8868\n    def reverseList(self, head: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        pre, cur = None, head\n        while cur:\n            nxt = cur.next\n            cur.next = pre\n            pre = cur\n            cur = nxt\n        return pre\n\n    def isPalindrome(self, head: Optional&#91;ListNode]) -&gt; bool:\n        mid = self.middleNode(head)\n        head2 = self.reverseList(mid)\n        while head2:\n            if head.val != head2.val:  # \u4e0d\u662f\u56de\u6587\u94fe\u8868\n                return False\n            head = head.next\n            head2 = head2.next\n        return True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">25.<a href=\"https:\/\/leetcode.cn\/problems\/linked-list-cycle\/\" target=\"_blank\"  rel=\"nofollow\" >\u73af\u5f62\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>&nbsp;\uff0c\u5224\u65ad\u94fe\u8868\u4e2d\u662f\u5426\u6709\u73af\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u94fe\u8868\u4e2d\u6709\u67d0\u4e2a\u8282\u70b9\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fde\u7eed\u8ddf\u8e2a&nbsp;<code>next<\/code>&nbsp;\u6307\u9488\u518d\u6b21\u5230\u8fbe\uff0c\u5219\u94fe\u8868\u4e2d\u5b58\u5728\u73af\u3002 \u4e3a\u4e86\u8868\u793a\u7ed9\u5b9a\u94fe\u8868\u4e2d\u7684\u73af\uff0c\u8bc4\u6d4b\u7cfb\u7edf\u5185\u90e8\u4f7f\u7528\u6574\u6570&nbsp;<code>pos<\/code>&nbsp;\u6765\u8868\u793a\u94fe\u8868\u5c3e\u8fde\u63a5\u5230\u94fe\u8868\u4e2d\u7684\u4f4d\u7f6e\uff08\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff09\u3002<strong>\u6ce8\u610f\uff1a<code>pos<\/code>&nbsp;\u4e0d\u4f5c\u4e3a\u53c2\u6570\u8fdb\u884c\u4f20\u9012&nbsp;<\/strong>\u3002\u4ec5\u4ec5\u662f\u4e3a\u4e86\u6807\u8bc6\u94fe\u8868\u7684\u5b9e\u9645\u60c5\u51b5\u3002<\/p>\n\n\n\n<p><em>\u5982\u679c\u94fe\u8868\u4e2d\u5b58\u5728\u73af<\/em>&nbsp;\uff0c\u5219\u8fd4\u56de&nbsp;<code>true<\/code>&nbsp;\u3002 \u5426\u5219\uff0c\u8fd4\u56de&nbsp;<code>false<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2018\/12\/07\/circularlinkedlist.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [3,2,0,-4], pos = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n<strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u4e2d\u6709\u4e00\u4e2a\u73af\uff0c\u5176\u5c3e\u90e8\u8fde\u63a5\u5230\u7b2c\u4e8c\u4e2a\u8282\u70b9\u3002<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5feb\u6162\u6307\u9488\uff0c\u5feb\u6307\u9488\u8ffd\u4e0a\u6162\u6307\u9488\u5219\u8bf4\u660e\u6709\u73af<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def hasCycle(self, head: Optional&#91;ListNode]) -&gt; bool:\n        slow=head\n        fast=head\n        while fast and fast.next:\n            slow=slow.next\n            fast=fast.next.next\n            if fast==slow:\n                return True\n        return False<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">26.<a href=\"https:\/\/leetcode.cn\/problems\/linked-list-cycle-ii\/\" target=\"_blank\"  rel=\"nofollow\" >\u73af\u5f62\u94fe\u8868 II<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\u7684\u5934\u8282\u70b9 &nbsp;<code>head<\/code>&nbsp;\uff0c\u8fd4\u56de\u94fe\u8868\u5f00\u59cb\u5165\u73af\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002&nbsp;<em>\u5982\u679c\u94fe\u8868\u65e0\u73af\uff0c\u5219\u8fd4\u56de&nbsp;<code>null<\/code>\u3002<\/em><\/p>\n\n\n\n<p>\u5982\u679c\u94fe\u8868\u4e2d\u6709\u67d0\u4e2a\u8282\u70b9\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fde\u7eed\u8ddf\u8e2a&nbsp;<code>next<\/code>&nbsp;\u6307\u9488\u518d\u6b21\u5230\u8fbe\uff0c\u5219\u94fe\u8868\u4e2d\u5b58\u5728\u73af\u3002 \u4e3a\u4e86\u8868\u793a\u7ed9\u5b9a\u94fe\u8868\u4e2d\u7684\u73af\uff0c\u8bc4\u6d4b\u7cfb\u7edf\u5185\u90e8\u4f7f\u7528\u6574\u6570&nbsp;<code>pos<\/code>&nbsp;\u6765\u8868\u793a\u94fe\u8868\u5c3e\u8fde\u63a5\u5230\u94fe\u8868\u4e2d\u7684\u4f4d\u7f6e\uff08<strong>\u7d22\u5f15\u4ece 0 \u5f00\u59cb<\/strong>\uff09\u3002\u5982\u679c&nbsp;<code>pos<\/code>&nbsp;\u662f&nbsp;<code>-1<\/code>\uff0c\u5219\u5728\u8be5\u94fe\u8868\u4e2d\u6ca1\u6709\u73af\u3002<strong>\u6ce8\u610f\uff1a<code>pos<\/code>&nbsp;\u4e0d\u4f5c\u4e3a\u53c2\u6570\u8fdb\u884c\u4f20\u9012<\/strong>\uff0c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u6807\u8bc6\u94fe\u8868\u7684\u5b9e\u9645\u60c5\u51b5\u3002<\/p>\n\n\n\n<p><strong>\u4e0d\u5141\u8bb8\u4fee\u6539&nbsp;<\/strong>\u94fe\u8868\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/07\/circularlinkedlist.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [3,2,0,-4], pos = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>\u8fd4\u56de\u7d22\u5f15\u4e3a 1 \u7684\u94fe\u8868\u8282\u70b9\n<strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u4e2d\u6709\u4e00\u4e2a\u73af\uff0c\u5176\u5c3e\u90e8\u8fde\u63a5\u5230\u7b2c\u4e8c\u4e2a\u8282\u70b9\u3002<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic.leetcode.cn\/1741414978-wPTZwJ-lc142-3-c.png\" alt=\"\u56fe\u89e3\u73af\u5f62\u94fe\u8868\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def detectCycle(self, head: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        slow=head\n        fast=head\n        while fast and fast.next:\n            slow=slow.next\n            fast=fast.next.next\n            if slow==fast:\n                temp=head\n                while temp!=slow:\n                    temp=temp.next\n                    slow=slow.next\n                return slow\n        return None<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">27.<a href=\"https:\/\/leetcode.cn\/problems\/merge-two-sorted-lists\/\" target=\"_blank\"  rel=\"nofollow\" >\u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u5c06\u4e24\u4e2a\u5347\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u94fe\u8868\u5e76\u8fd4\u56de\u3002\u65b0\u94fe\u8868\u662f\u901a\u8fc7\u62fc\u63a5\u7ed9\u5b9a\u7684\u4e24\u4e2a\u94fe\u8868\u7684\u6240\u6709\u8282\u70b9\u7ec4\u6210\u7684\u3002&nbsp;<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/merge_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>l1 = [1,2,4], l2 = [1,3,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,1,2,3,4,4]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4f9d\u6b21\u904d\u5386\u6bd4\u5927\u5c0f\u63d2\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def mergeTwoLists(self, list1: Optional&#91;ListNode], list2: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        l1=list1\n        l2=list2\n        cur=head=ListNode()\n        while l1 and l2:\n            if l1.val&lt;l2.val:\n                cur.next=l1\n                l1=l1.next\n                cur=cur.next\n            else :\n                cur.next=l2\n                l2=l2.next\n                cur=cur.next\n        cur.next=l1 or l2\n        return head.next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">28.<a href=\"https:\/\/leetcode.cn\/problems\/add-two-numbers\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e24\u6570\u76f8\u52a0<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e24\u4e2a&nbsp;<strong>\u975e\u7a7a<\/strong>&nbsp;\u7684\u94fe\u8868\uff0c\u8868\u793a\u4e24\u4e2a\u975e\u8d1f\u7684\u6574\u6570\u3002\u5b83\u4eec\u6bcf\u4f4d\u6570\u5b57\u90fd\u662f\u6309\u7167&nbsp;<strong>\u9006\u5e8f<\/strong>&nbsp;\u7684\u65b9\u5f0f\u5b58\u50a8\u7684\uff0c\u5e76\u4e14\u6bcf\u4e2a\u8282\u70b9\u53ea\u80fd\u5b58\u50a8&nbsp;<strong>\u4e00\u4f4d<\/strong>&nbsp;\u6570\u5b57\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u5c06\u4e24\u4e2a\u6570\u76f8\u52a0\uff0c\u5e76\u4ee5\u76f8\u540c\u5f62\u5f0f\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u548c\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u9664\u4e86\u6570\u5b57 0 \u4e4b\u5916\uff0c\u8fd9\u4e24\u4e2a\u6570\u90fd\u4e0d\u4f1a\u4ee5 0&nbsp;\u5f00\u5934\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2021\/01\/02\/addtwonumber1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>l1 = [2,4,3], l2 = [5,6,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>[7,0,8]\n<strong>\u89e3\u91ca\uff1a<\/strong>342 + 465 = 807.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u6570\u503c\u53ef\u80fd\u8d8a\u754c\uff0c\u56e0\u6b64\u4e0d\u80fd\u6574\u4e2a\u6570\u8bfb\u51fa\u6765\u52a0\u518d\u62c6\u89e3\u3002\u5728\u6b64\u9010\u4f4d\u52a0\u5e76\u8ba1\u7b97\u8fdb\u4f4d\uff0c%10\u6c42\u4f59\u8d4b\u503c\uff0c\/\/10\u8fdb\u4f4d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def addTwoNumbers(self, l1: Optional&#91;ListNode], l2: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        carry=0\n        head=cur=ListNode()\n        while l1 or l2 or carry:\n            if l1:\n                carry += l1.val  # \u8282\u70b9\u503c\u548c\u8fdb\u4f4d\u52a0\u5728\u4e00\u8d77\n                l1 = l1.next  # \u4e0b\u4e00\u4e2a\u8282\u70b9\n            if l2:\n                carry += l2.val  # \u8282\u70b9\u503c\u548c\u8fdb\u4f4d\u52a0\u5728\u4e00\u8d77\n                l2 = l2.next  # \u4e0b\u4e00\u4e2a\u8282\u70b9\n            cur.next = ListNode(carry % 10)  # \u6bcf\u4e2a\u8282\u70b9\u4fdd\u5b58\u4e00\u4e2a\u6570\u4f4d\n            carry \/\/= 10  # \u65b0\u7684\u8fdb\u4f4d\n            cur = cur.next  # \u4e0b\u4e00\u4e2a\u8282\u70b9\n        return head.next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">29.<a href=\"https:\/\/leetcode.cn\/problems\/remove-nth-node-from-end-of-list\/\" target=\"_blank\"  rel=\"nofollow\" >\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c&nbsp;<code>n<\/code><em>&nbsp;<\/em>\u4e2a\u7ed3\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/remove_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5], n = 2<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,5]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u94fe\u8868\u603b\u957f\u4e3aL\uff0c\u5b9a\u5de6\u53f3\u6307\u9488\uff0c\u53f3\u6307\u9488\u5148\u8d70n\uff0c\u5de6\u53f3\u6307\u9488\u518d\u540c\u6b65\u8d70\u5269\u4f59\u7684L-n,\u5219\u5de6\u6307\u9488\u6307\u5411\u7684\u4fbf\u662f\u5012\u6570\u7b2cn\u4e2a\u8282\u70b9\uff0c\u4e3a\u907f\u514d\u5934\u8282\u70b9\u88ab\u5220\u9664\uff0c\u5b9a\u4e49dummy\u6307\u9488\uff0cdummy.next\u6307\u5411head<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def removeNthFromEnd(self, head: Optional&#91;ListNode], n: int) -&gt; Optional&#91;ListNode]:\n        left=right=dummy=ListNode(next=head)\n        for i in range(n):\n            right=right.next\n        while right.next:\n            left=left.next\n            right=right.next\n        left.next=left.next.next\n        return dummy.next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">30.<a href=\"https:\/\/leetcode.cn\/problems\/swap-nodes-in-pairs\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e24\u4e24\u4ea4\u6362\u94fe\u8868\u4e2d\u7684\u8282\u70b9<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u4e24\u4e24\u4ea4\u6362\u5176\u4e2d\u76f8\u90bb\u7684\u8282\u70b9\uff0c\u5e76\u8fd4\u56de\u4ea4\u6362\u540e\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002\u4f60\u5fc5\u987b\u5728\u4e0d\u4fee\u6539\u8282\u70b9\u5185\u90e8\u7684\u503c\u7684\u60c5\u51b5\u4e0b\u5b8c\u6210\u672c\u9898\uff08\u5373\uff0c\u53ea\u80fd\u8fdb\u884c\u8282\u70b9\u4ea4\u6362\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/swap_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[2,1,4,3]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5b9a\u4e49dummy\u56fa\u5b9a\u94fe\u8868\u5934\uff0cpre\u4e3a\u4ea4\u6362\u524d\u4e00\u4e2a\u8282\u70b9\uff0cleft\u4e3a\u4ea4\u6362\u5de6\u8282\u70b9\uff0cright\u4e3a\u4ea4\u6362\u53f3\u8282\u70b9\uff0c\u5de6\u53f3\u8282\u70b9\u5747\u4e0d\u4e3a\u7a7a\u8fdb\u884c\u8282\u70b9\u4ea4\u6362<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def swapPairs(self, head: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        pre=dummy=left=right=ListNode(next=head)\n        if head==None or head.next==None:\n            return head\n        left=pre.next\n        right=pre.next.next\n        while left and right:\n            pre.next=right\n            left.next=right.next\n            right.next=left\n            pre=left\n            left=left.next\n            right=left.next if left else None\n        return dummy.next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">31.<a href=\"https:\/\/leetcode.cn\/problems\/reverse-nodes-in-k-group\/\" target=\"_blank\"  rel=\"nofollow\" >K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>&nbsp;\uff0c\u6bcf&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u4e2a\u8282\u70b9\u4e00\u7ec4\u8fdb\u884c\u7ffb\u8f6c\uff0c\u8bf7\u4f60\u8fd4\u56de\u4fee\u6539\u540e\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p><code>k<\/code>&nbsp;\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u5b83\u7684\u503c\u5c0f\u4e8e\u6216\u7b49\u4e8e\u94fe\u8868\u7684\u957f\u5ea6\u3002\u5982\u679c\u8282\u70b9\u603b\u6570\u4e0d\u662f&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u7684\u6574\u6570\u500d\uff0c\u90a3\u4e48\u8bf7\u5c06\u6700\u540e\u5269\u4f59\u7684\u8282\u70b9\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u3002<\/p>\n\n\n\n<p>\u4f60\u4e0d\u80fd\u53ea\u662f\u5355\u7eaf\u7684\u6539\u53d8\u8282\u70b9\u5185\u90e8\u7684\u503c\uff0c\u800c\u662f\u9700\u8981\u5b9e\u9645\u8fdb\u884c\u8282\u70b9\u4ea4\u6362\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/reverse_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5], k = 2<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[2,1,4,3,5]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>1.\u904d\u5386\u94fe\u8868\uff0c\u770b\u770b\u9700\u8981\u53cd\u8f6c\u591a\u5c11\u7ec4\uff08n\/k\uff09<\/p>\n\n\n\n<p>2.\u5c40\u90e8\u53cd\u8f6c\u540e\u8fd8\u9700\u8981\u94fe\u63a5\uff0c\u56e0\u6b64\u9700\u8981\u7559\u4e0b\u94fe\u63a5\u951a\u70b9\uff0c\u5b9a\u4e49\u94fe\u63a5\u951a\u70b9p0,p0.next\u4e3a\u672a\u53cd\u8f6c\u7684\u5c40\u90e8\u94fe\u8868\u8d77\u70b9<\/p>\n\n\n\n<p>3.\u53cd\u8f6c\u5c40\u90e8\u94fe\u8868\uff0c\u5b8c\u6210\u4e00\u7ec4\u53cd\u8f6c\u540e\u901a\u8fc7\u94fe\u63a5\u951a\u70b9\u91cd\u65b0\u94fe\u63a5\uff0c\u5e76\u5c06\u951a\u70b9\u8df3\u8f6c\u81f3\u4e0b\u4e00\u7ec4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def reverseKGroup(self, head: Optional&#91;ListNode], k: int) -&gt; Optional&#91;ListNode]:\n        n=0\n        cur=head\n        while cur:\n            n+=1\n            cur=cur.next\n        p0=dummy=ListNode(next=head)\n        pre=None\n        cur=head\n        while n&gt;=k:\n            n-=k \n            #\u53cd\u8f6c\u5c40\u90e8\u94fe\u8868\n            for i in range(k):\n                nxt=cur.next\n                cur.next=pre\n                pre=cur\n                cur=nxt\n            #\u8df3\u8f6c\u951a\u70b9\n            nxt=p0.next\n            nxt.next=cur\n            p0.next=pre\n            p0=nxt\n        return dummy.next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">32.<a href=\"https:\/\/leetcode.cn\/problems\/copy-list-with-random-pointer\/\" target=\"_blank\"  rel=\"nofollow\" >\u968f\u673a\u94fe\u8868\u7684\u590d\u5236<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a&nbsp;<code>n<\/code>&nbsp;\u7684\u94fe\u8868\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u989d\u5916\u589e\u52a0\u7684\u968f\u673a\u6307\u9488&nbsp;<code>random<\/code>&nbsp;\uff0c\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411\u94fe\u8868\u4e2d\u7684\u4efb\u4f55\u8282\u70b9\u6216\u7a7a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u6784\u9020\u8fd9\u4e2a\u94fe\u8868\u7684&nbsp;<strong><a href=\"https:\/\/baike.baidu.com\/item\/%E6%B7%B1%E6%8B%B7%E8%B4%9D\/22785317?fr=aladdin\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u6df1\u62f7\u8d1d<\/a><\/strong>\u3002&nbsp;\u6df1\u62f7\u8d1d\u5e94\u8be5\u6b63\u597d\u7531&nbsp;<code>n<\/code>&nbsp;\u4e2a&nbsp;<strong>\u5168\u65b0<\/strong>&nbsp;\u8282\u70b9\u7ec4\u6210\uff0c\u5176\u4e2d\u6bcf\u4e2a\u65b0\u8282\u70b9\u7684\u503c\u90fd\u8bbe\u4e3a\u5176\u5bf9\u5e94\u7684\u539f\u8282\u70b9\u7684\u503c\u3002\u65b0\u8282\u70b9\u7684&nbsp;<code>next<\/code>&nbsp;\u6307\u9488\u548c&nbsp;<code>random<\/code>&nbsp;\u6307\u9488\u4e5f\u90fd\u5e94\u6307\u5411\u590d\u5236\u94fe\u8868\u4e2d\u7684\u65b0\u8282\u70b9\uff0c\u5e76\u4f7f\u539f\u94fe\u8868\u548c\u590d\u5236\u94fe\u8868\u4e2d\u7684\u8fd9\u4e9b\u6307\u9488\u80fd\u591f\u8868\u793a\u76f8\u540c\u7684\u94fe\u8868\u72b6\u6001\u3002<strong>\u590d\u5236\u94fe\u8868\u4e2d\u7684\u6307\u9488\u90fd\u4e0d\u5e94\u6307\u5411\u539f\u94fe\u8868\u4e2d\u7684\u8282\u70b9&nbsp;<\/strong>\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u539f\u94fe\u8868\u4e2d\u6709&nbsp;<code>X<\/code>&nbsp;\u548c&nbsp;<code>Y<\/code>&nbsp;\u4e24\u4e2a\u8282\u70b9\uff0c\u5176\u4e2d&nbsp;<code>X.random --&gt; Y<\/code>&nbsp;\u3002\u90a3\u4e48\u5728\u590d\u5236\u94fe\u8868\u4e2d\u5bf9\u5e94\u7684\u4e24\u4e2a\u8282\u70b9&nbsp;<code>x<\/code>&nbsp;\u548c&nbsp;<code>y<\/code>&nbsp;\uff0c\u540c\u6837\u6709&nbsp;<code>x.random --&gt; y<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u590d\u5236\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u7528\u4e00\u4e2a\u7531&nbsp;<code>n<\/code>&nbsp;\u4e2a\u8282\u70b9\u7ec4\u6210\u7684\u94fe\u8868\u6765\u8868\u793a\u8f93\u5165\/\u8f93\u51fa\u4e2d\u7684\u94fe\u8868\u3002\u6bcf\u4e2a\u8282\u70b9\u7528\u4e00\u4e2a&nbsp;<code>[val, random_index]<\/code>&nbsp;\u8868\u793a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>val<\/code>\uff1a\u4e00\u4e2a\u8868\u793a&nbsp;<code>Node.val<\/code>&nbsp;\u7684\u6574\u6570\u3002<\/li>\n\n\n\n<li><code>random_index<\/code>\uff1a\u968f\u673a\u6307\u9488\u6307\u5411\u7684\u8282\u70b9\u7d22\u5f15\uff08\u8303\u56f4\u4ece&nbsp;<code>0<\/code>&nbsp;\u5230&nbsp;<code>n-1<\/code>\uff09\uff1b\u5982\u679c\u4e0d\u6307\u5411\u4efb\u4f55\u8282\u70b9\uff0c\u5219\u4e3a&nbsp;&nbsp;<code>null<\/code>&nbsp;\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u7684\u4ee3\u7801&nbsp;<strong>\u53ea<\/strong>&nbsp;\u63a5\u53d7\u539f\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>&nbsp;\u4f5c\u4e3a\u4f20\u5165\u53c2\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [[7,null],[13,0],[11,4],[10,2],[1,0]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[7,null],[13,0],[11,4],[10,2],[1,0]]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u89e3\u6cd51\uff1a\u54c8\u5e0c\u5b58\u50a8<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import collections\nclass Solution:\n    def copyRandomList(self, head: 'Optional&#91;Node]') -&gt; 'Optional&#91;Node]':\n        L_dict=collections.defaultdict()\n        if head==None:\n            return None\n        L=head\n        while L:\n            L_dict&#91;L]=Node(L.val)\n            L=L.next\n        L=head\n        while L:\n            L_dict&#91;L].next=L_dict.get(L.next)\n            L_dict&#91;L].random=L_dict.get(L.random)\n            L=L.next\n        return L_dict&#91;head]<\/code><\/pre>\n\n\n\n<p>\u89e3\u6cd52\uff1a\u6211\u4eec\u53ef\u4ee5\u628a\u65b0\u94fe\u8868\u548c\u65e7\u94fe\u8868\u300c\u6df7\u5728\u4e00\u8d77\u300d\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u94fe\u8868 1\u21922\u21923\uff0c\u4f9d\u6b21\u590d\u5236\u6bcf\u4e2a\u8282\u70b9\uff08\u521b\u5efa\u65b0\u8282\u70b9\u5e76\u590d\u5236 val \u548c next\uff09\uff0c\u628a\u65b0\u8282\u70b9\u76f4\u63a5\u63d2\u5230\u539f\u8282\u70b9\u7684\u540e\u9762\uff0c\u5f62\u6210\u4e00\u4e2a\u4ea4\u9519\u94fe\u8868\uff1a<\/p>\n\n\n\n<p>1\u21921\u2032\u21922\u21922\u2032\u21923\u21923\u2032<\/p>\n\n\n\n<p>\u5982\u6b64\u4e00\u6765\uff0c\u539f\u94fe\u8868\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u5c31\u662f\u5176\u5bf9\u5e94\u7684\u65b0\u94fe\u8868\u8282\u70b9\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def copyRandomList(self, head: 'Optional&#91;Node]') -&gt; 'Optional&#91;Node]':\n        # \u590d\u5236\u6bcf\u4e2a\u8282\u70b9\uff0c\u628a\u65b0\u8282\u70b9\u76f4\u63a5\u63d2\u5230\u539f\u8282\u70b9\u7684\u540e\u9762\n        cur = head\n        while cur:\n            cur.next = Node(cur.val, cur.next)\n            cur = cur.next.next\n\n        # \u904d\u5386\u4ea4\u9519\u94fe\u8868\u4e2d\u7684\u539f\u94fe\u8868\u8282\u70b9\n        cur = head\n        while cur:\n            if cur.random:\n                # \u8981\u590d\u5236\u7684 random \u662f cur.random \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n                cur.next.random = cur.random.next\n            cur = cur.next.next\n\n        # \u628a\u4ea4\u9519\u94fe\u8868\u5206\u79bb\u6210\u4e24\u4e2a\u94fe\u8868\n        tail = dummy = Node(0, head)\n        cur = head\n        while cur:\n            copy = cur.next  # \u65b0\u8282\u70b9\n            tail.next = copy  # \u628a\u65b0\u8282\u70b9\u63d2\u5728 tail \u7684\u540e\u9762\uff0c\u6784\u5efa\u65b0\u7684\u94fe\u8868\n            cur.next = copy.next  # \u6062\u590d\u539f\u8282\u70b9\u7684 next\n            cur = cur.next\n            tail = tail.next\n\n        return dummy.next<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">33.<a href=\"https:\/\/leetcode.cn\/problems\/sort-list\/\" target=\"_blank\"  rel=\"nofollow\" >\u6392\u5e8f\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u94fe\u8868\u7684\u5934\u7ed3\u70b9&nbsp;<code>head<\/code>&nbsp;\uff0c\u8bf7\u5c06\u5176\u6309&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u6392\u5217\u5e76\u8fd4\u56de&nbsp;<strong>\u6392\u5e8f\u540e\u7684\u94fe\u8868<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/14\/sort_list_1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>head = [4,2,1,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,4]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5206\u6cbb\u6392\u5e8f\u5728\u94fe\u8868\u4e2d\u5e94\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def middleNode(self,head:Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        slow=head\n        fast=head\n        while fast and fast.next:\n            pre=slow\n            slow=slow.next\n            fast=fast.next.next\n        pre.next=None\n        return slow\n\n    def mergeTwoLists(self,list1:Optional&#91;ListNode],list2:Optional&#91;ListNode]):\n        cur=dummy=ListNode()\n        while list1 and list2:\n            if list1.val&lt;list2.val:\n                cur.next=list1\n                cur=cur.next\n                list1=list1.next\n            else:\n                cur.next=list2\n                cur=cur.next\n                list2=list2.next\n        cur.next=list1 if list1 else list2\n        return dummy.next\n\n\n    def sortList(self, head: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        if head==None or head.next==None:\n            return head\n        head2=self.middleNode(head)\n        head=self.sortList(head)\n        head2=self.sortList(head2)\n        return self.mergeTwoLists(head,head2)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">34.<a href=\"https:\/\/leetcode.cn\/problems\/merge-k-sorted-lists\/\" target=\"_blank\"  rel=\"nofollow\" >\u5408\u5e76 K \u4e2a\u5347\u5e8f\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\u6570\u7ec4\uff0c\u6bcf\u4e2a\u94fe\u8868\u90fd\u5df2\u7ecf\u6309\u5347\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u5c06\u6240\u6709\u94fe\u8868\u5408\u5e76\u5230\u4e00\u4e2a\u5347\u5e8f\u94fe\u8868\u4e2d\uff0c\u8fd4\u56de\u5408\u5e76\u540e\u7684\u94fe\u8868\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>lists = [[1,4,5],[1,3,4],[2,6]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,1,2,3,4,4,5,6]<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u6570\u7ec4\u5982\u4e0b\uff1a<\/li>\n\n\n\n<li>[<\/li>\n\n\n\n<li>  1-&gt;4-&gt;5,<\/li>\n\n\n\n<li>  1-&gt;3-&gt;4,<\/li>\n\n\n\n<li>  2-&gt;6<\/li>\n\n\n\n<li>]<\/li>\n\n\n\n<li>\u5c06\u5b83\u4eec\u5408\u5e76\u5230\u4e00\u4e2a\u6709\u5e8f\u94fe\u8868\u4e2d\u5f97\u5230\u3002<\/li>\n\n\n\n<li>1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5206\u6cbb\u9012\u5f52<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    # \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868\n    def mergeTwoLists(self, list1: Optional&#91;ListNode], list2: Optional&#91;ListNode]) -&gt; Optional&#91;ListNode]:\n        cur = dummy = ListNode()  # \u7528\u54e8\u5175\u8282\u70b9\u7b80\u5316\u4ee3\u7801\u903b\u8f91\n        while list1 and list2:\n            if list1.val &lt; list2.val:\n                cur.next = list1  # \u628a list1 \u52a0\u5230\u65b0\u94fe\u8868\u4e2d\n                list1 = list1.next\n            else:  # \u6ce8\uff1a\u76f8\u7b49\u7684\u60c5\u51b5\u52a0\u54ea\u4e2a\u8282\u70b9\u90fd\u662f\u53ef\u4ee5\u7684\n                cur.next = list2  # \u628a list2 \u52a0\u5230\u65b0\u94fe\u8868\u4e2d\n                list2 = list2.next\n            cur = cur.next\n        cur.next = list1 if list1 else list2  # \u62fc\u63a5\u5269\u4f59\u94fe\u8868\n        return dummy.next\n\n    def mergeKLists(self, lists: List&#91;Optional&#91;ListNode]]) -&gt; Optional&#91;ListNode]:\n        m = len(lists)\n        if m == 0:\n            return None\n        if m == 1:\n            return lists&#91;0]  # \u65e0\u9700\u5408\u5e76\uff0c\u76f4\u63a5\u8fd4\u56de\n        left = self.mergeKLists(lists&#91;:m \/\/ 2])  # \u5408\u5e76\u5de6\u534a\u90e8\u5206\n        right = self.mergeKLists(lists&#91;m \/\/ 2:])  # \u5408\u5e76\u53f3\u534a\u90e8\u5206\n        return self.mergeTwoLists(left, right)  # \u6700\u540e\u628a\u5de6\u534a\u548c\u53f3\u534a\u5408\u5e76<\/code><\/pre>\n\n\n\n<p>\u5206\u6cbb\u8fed\u4ee3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def megeTwoList(self, list1: Optional&#91;ListNode],list2: Optional&#91;ListNode])-&gt;Optional&#91;ListNode]:\n        cur=dummy=ListNode()\n        while list1 and list2:\n            if list1.val&lt;list2.val:\n                cur.next=list1\n                cur=cur.next\n                list1=list1.next\n            else:\n                cur.next=list2\n                cur=cur.next\n                list2=list2.next\n        cur.next=list1 if list1 else list2\n        return dummy.next\n\n    def mergeKLists(self, lists: List&#91;Optional&#91;ListNode]]) -&gt; Optional&#91;ListNode]:\n        n=len(lists)\n        if n==0:\n            return None\n        step=1\n        while step&lt;n:\n            for i in range(0,n-step,step*2):\n                lists&#91;i]=self.megeTwoList(lists&#91;i],lists&#91;i+step])\n            step*=2\n        return lists&#91;0]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">35.<a href=\"https:\/\/leetcode.cn\/problems\/lru-cache\/\" target=\"_blank\"  rel=\"nofollow\" >LRU \u7f13\u5b58<\/a><\/h2>\n\n\n\n<p>\u8bf7\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u6ee1\u8db3&nbsp;&nbsp;<a href=\"https:\/\/baike.baidu.com\/item\/LRU\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >LRU (\u6700\u8fd1\u6700\u5c11\u4f7f\u7528) \u7f13\u5b58<\/a>&nbsp;\u7ea6\u675f\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0&nbsp;<code>LRUCache<\/code>&nbsp;\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>LRUCache(int capacity)<\/code>&nbsp;\u4ee5&nbsp;<strong>\u6b63\u6574\u6570<\/strong>&nbsp;\u4f5c\u4e3a\u5bb9\u91cf&nbsp;<code>capacity<\/code>&nbsp;\u521d\u59cb\u5316 LRU \u7f13\u5b58<\/li>\n\n\n\n<li><code>int get(int key)<\/code>&nbsp;\u5982\u679c\u5173\u952e\u5b57&nbsp;<code>key<\/code>&nbsp;\u5b58\u5728\u4e8e\u7f13\u5b58\u4e2d\uff0c\u5219\u8fd4\u56de\u5173\u952e\u5b57\u7684\u503c\uff0c\u5426\u5219\u8fd4\u56de&nbsp;<code>-1<\/code>&nbsp;\u3002<\/li>\n\n\n\n<li><code>void put(int key, int value)<\/code>&nbsp;\u5982\u679c\u5173\u952e\u5b57&nbsp;<code>key<\/code>&nbsp;\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u53d8\u66f4\u5176\u6570\u636e\u503c&nbsp;<code>value<\/code>&nbsp;\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5411\u7f13\u5b58\u4e2d\u63d2\u5165\u8be5\u7ec4&nbsp;<code>key-value<\/code>&nbsp;\u3002\u5982\u679c\u63d2\u5165\u64cd\u4f5c\u5bfc\u81f4\u5173\u952e\u5b57\u6570\u91cf\u8d85\u8fc7&nbsp;<code>capacity<\/code>&nbsp;\uff0c\u5219\u5e94\u8be5&nbsp;<strong>\u9010\u51fa<\/strong>&nbsp;\u6700\u4e45\u672a\u4f7f\u7528\u7684\u5173\u952e\u5b57\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u51fd\u6570&nbsp;<code>get<\/code>&nbsp;\u548c&nbsp;<code>put<\/code>&nbsp;\u5fc5\u987b\u4ee5&nbsp;<code>O(1)<\/code>&nbsp;\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u8fd0\u884c\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165<\/strong><\/li>\n\n\n\n<li>[\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"]<\/li>\n\n\n\n<li>[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa<\/strong><\/li>\n\n\n\n<li>[null, null, null, 1, null, -1, null, -1, 3, 4]<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong> LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); \/\/ \u7f13\u5b58\u662f {1=1} <br>lRUCache.put(2, 2); \/\/ \u7f13\u5b58\u662f {1=1, 2=2} <br>lRUCache.get(1); \/\/ \u8fd4\u56de 1 <br>lRUCache.put(3, 3); \/\/ \u8be5\u64cd\u4f5c\u4f1a\u4f7f\u5f97\u5173\u952e\u5b57 2 \u4f5c\u5e9f\uff0c\u7f13\u5b58\u662f {1=1, 3=3} <br>lRUCache.get(2); \/\/ \u8fd4\u56de -1 (\u672a\u627e\u5230) <br>lRUCache.put(4, 4); \/\/ \u8be5\u64cd\u4f5c\u4f1a\u4f7f\u5f97\u5173\u952e\u5b57 1 \u4f5c\u5e9f\uff0c\u7f13\u5b58\u662f {4=4, 3=3} <br>lRUCache.get(1); \/\/ \u8fd4\u56de -1 (\u672a\u627e\u5230) <br>lRUCache.get(3); \/\/ \u8fd4\u56de 3 <br>lRUCache.get(4); \/\/ \u8fd4\u56de 4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u6784\u5efa\u6570\u636e\u7ed3\u6784\uff0cdummy\u540c\u65f6\u4f5c\u4e3a\u94fe\u8868\u5934\u548c\u5c3e\uff0c\u6784\u5efa\u54c8\u5e0c\u8868\u5b58\u50a8key\uff1aNode\u7684\u6620\u5c04<br>\u8bfb\u53d6\u503c\u65f6\u5c06\u5148\u7528\u54c8\u5e0c\u8868\u5224\u5b9a\u503c\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u5c06\u8282\u70b9\u53d6\u51fa\u5e76\u653e\u7f6e\u5230\u94fe\u8868\u5934dummy\u4e4b\u540e\uff0c\u4e0d\u5b58\u5728\u5219\u8fd4\u56de-1<br>\u653e\u7f6e\u503c\u65f6\u5148\u7528\u54c8\u5e0c\u8868\u5224\u5b9a\u503c\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u5c06node.val\u8986\u76d6\u65b0\u503c\uff0c\u5e76\u524d\u7f6e\u8282\u70b9\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u65b0\u8282\u70b9\u7f6e\u4e8e\u94fe\u8868\u5934dummy\u4e4b\u540e\u3002\u54c8\u5e0c\u8868key_to_node\u540c\u6b65\u66f4\u65b0\u3002\u82e5\u5b58\u91cf\u7f13\u5b58\u6570\u5927\u4e8e<code>capacity<\/code>\uff0c\u5219\u5220\u9664dummy\u7684prev\u8282\u70b9\uff08\u94fe\u8868\u5c3e\u8282\u70b9\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Node:\n    def __init__(self,key=0,val=0):\n        self.val=val\n        self.key=key\n        self.prev=None\n        self.next=None\n\nclass LRUCache:\n    def out_node(self,node:Node)-&gt; None:\n        node.prev.next=node.next\n        node.next.prev=node.prev\n\n\n    def in_node(self,node:Node)-&gt; None:\n        node.prev=self.dummy\n        node.next=self.dummy.next\n        self.dummy.next.prev=node\n        self.dummy.next=node\n\n\n    def __init__(self, capacity: int):\n        self.capacity=capacity\n        self.dummy=Node()\n        self.dummy.prev=self.dummy\n        self.dummy.next=self.dummy\n        self.key_to_node={}\n        \n\n    def get(self, key: int) -&gt; int:\n        if key in self.key_to_node:\n            node = self.key_to_node&#91;key]\n            self.out_node(node)\n            self.in_node(node)\n            return node.val\n        else:\n            return -1\n        \n\n    def put(self, key: int, value: int) -&gt; None:\n        if key in self.key_to_node:\n            node = self.key_to_node&#91;key]\n            node.val=value\n            self.out_node(node)\n            self.in_node(node)\n        else:\n            node=Node(key,value)\n            self.in_node(node)\n            self.key_to_node&#91;key]=node\n        if len(self.key_to_node)&gt;self.capacity:\n            node=self.dummy.prev\n            self.out_node(node)\n            del self.key_to_node&#91;node.key]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">36.<a href=\"https:\/\/leetcode.cn\/problems\/binary-tree-inorder-traversal\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u8fd4\u56de&nbsp;<em>\u5b83\u7684&nbsp;<strong>\u4e2d\u5e8f<\/strong>&nbsp;\u904d\u5386<\/em>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/15\/inorder_1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [1,null,2,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,3,2]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u524d\u5e8f\u904d\u5386\uff1a\u6839-\u5de6-\u53f3\uff0c\u5148\u83b7\u53d6\u6839\u8282\u70b9\u503c\uff0c\u518d\u8bbf\u95ee\u6839\u7684\u5de6\u5b50\u6811\uff0c\u6700\u540e\u8bbf\u95ee\u6839\u7684\u53f3\u5b50\u6811<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def inorderTraversal(self, root: Optional&#91;TreeNode]) -&gt; List&#91;int]:\n        def dfs(node:Optional&#91;TreeNode])-&gt;None:\n            if node is None:\n                return \n            ans.append(node.val)\n            dfs(node.left)\n            dfs(node.right)\n\n        ans=&#91;]\n        dfs(root)\n        return ans<\/code><\/pre>\n\n\n\n<p>\u4e2d\u5e8f\u904d\u5386\uff1a\u5de6-\u6839-\u53f3\u3002\u5148\u8bbf\u95ee\u6839\u7684\u5de6\u5b50\u6811\uff0c\u518d\u83b7\u53d6\u6839\u8282\u70b9\u503c\uff0c\u6700\u540e\u8bbf\u95ee\u6839\u7684\u53f3\u5b50\u6811<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def inorderTraversal(self, root: Optional&#91;TreeNode]) -&gt; List&#91;int]:\n        def dfs(node:Optional&#91;TreeNode])-&gt;None:\n            if node is None:\n                return \n            dfs(node.left)\n            ans.append(node.val)\n            dfs(node.right)\n\n        ans=&#91;]\n        dfs(root)\n        return ans<\/code><\/pre>\n\n\n\n<p>\u540e\u5e8f\u904d\u5386\uff1a\u5de6-\u53f3-\u6839\u3002\u5148\u8bbf\u95ee\u6839\u7684\u5de6\u5b50\u6811\uff0c\u518d\u8bbf\u95ee\u6839\u7684\u53f3\u5b50\u6811\uff0c\u6700\u540e\u83b7\u53d6\u6839\u8282\u70b9\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def inorderTraversal(self, root: Optional&#91;TreeNode]) -&gt; List&#91;int]:\n        def dfs(node:Optional&#91;TreeNode])-&gt;None:\n            if node is None:\n                return \n            dfs(node.left)\n            dfs(node.right)\n            ans.append(node.val)\n\n        ans=&#91;]\n        dfs(root)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">37.<a href=\"https:\/\/leetcode.cn\/problems\/maximum-depth-of-binary-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u7684\u6700\u5927\u6df1\u5ea6<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811&nbsp;<code>root<\/code>&nbsp;\uff0c\u8fd4\u56de\u5176\u6700\u5927\u6df1\u5ea6\u3002<\/p>\n\n\n\n<p>\u4e8c\u53c9\u6811\u7684&nbsp;<strong>\u6700\u5927\u6df1\u5ea6<\/strong>&nbsp;\u662f\u6307\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u5b50\u8282\u70b9\u7684\u6700\u957f\u8def\u5f84\u4e0a\u7684\u8282\u70b9\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/26\/tmp-tree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [3,9,20,null,null,15,7]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>3<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a\u81ea\u5e95\u5411\u4e0a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxDepth(self, root: Optional&#91;TreeNode]) -&gt; int:\n        if root is None:\n            return 0\n        l_depth = self.maxDepth(root.left)\n        r_depth = self.maxDepth(root.right)\n        return max(l_depth, r_depth) + 1<\/code><\/pre>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff1a\u81ea\u9876\u5411\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxDepth(self, root: Optional&#91;TreeNode]) -&gt; int:\n        ans=0\n        def dfs(node: Optional&#91;TreeNode],level:int) -&gt; int:\n            if not node:\n                return \n            level+=1\n            nonlocal ans\n            ans=max(ans,level)\n            dfs(node.left,level)\n            dfs(node.right,level)\n        \n        dfs(root,0)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">38.<a href=\"https:\/\/leetcode.cn\/problems\/invert-binary-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u7ffb\u8f6c\u4e8c\u53c9\u6811<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u7ffb\u8f6c\u8fd9\u68f5\u4e8c\u53c9\u6811\uff0c\u5e76\u8fd4\u56de\u5176\u6839\u8282\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/14\/invert1-tree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [4,2,7,1,3,6,9]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[4,7,2,9,6,3,1]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u9012\u5f52\u5199\u6cd5\uff0c\u9012\u5f52\u7ffb\u8f6c\u5de6\u5b50\u6811\uff0c\u9012\u5f52\u7ffb\u8f6c\u53f3\u5b50\u6811\uff0c\u4ea4\u6362\u5de6\u53f3\u5b50\u6811<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def invertTree(self, root: Optional&#91;TreeNode]) -&gt; Optional&#91;TreeNode]:\n        if root==None:\n            return None\n        left=self.invertTree(root.left)\n        right=self.invertTree(root.right)\n        root.left=right\n        root.right=left\n        return root<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">39.<a href=\"https:\/\/leetcode.cn\/problems\/symmetric-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u5bf9\u79f0\u4e8c\u53c9\u6811<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c \u68c0\u67e5\u5b83\u662f\u5426\u8f74\u5bf9\u79f0\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic.leetcode.cn\/1698026966-JDYPDU-image.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [1,2,2,3,4,4,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u628a\u8f93\u5165\u7684\u4e8c\u53c9\u6811\u62c6\u5206\u6210\u5de6\u5b50\u6811&nbsp;<em>p<\/em>&nbsp;\u548c\u53f3\u5b50\u6811&nbsp;<em>q<\/em>\u3002\u6211\u4eec\u9700\u8981\u5224\u65ad&nbsp;<em>p<\/em>&nbsp;\u548c&nbsp;<em>q<\/em>&nbsp;\u662f\u5426\u4e92\u4e3a\u955c\u50cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def isSameTree(self,p:Optional&#91;TreeNode],q:Optional&#91;TreeNode])-&gt;bool:\n        if p==None or q==None:\n            return p is q\n        return p.val==q.val and self.isSameTree(p.left,q.right) and self.isSameTree(p.right,q.left)\n\n    def isSymmetric(self, root: Optional&#91;TreeNode]) -&gt; bool:\n        ans=self.isSameTree(root.left,root.right)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">40.<a href=\"https:\/\/leetcode.cn\/problems\/diameter-of-binary-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u7684\u76f4\u5f84<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u8fd4\u56de\u8be5\u6811\u7684&nbsp;<strong>\u76f4\u5f84<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p>\u4e8c\u53c9\u6811\u7684&nbsp;<strong>\u76f4\u5f84<\/strong>&nbsp;\u662f\u6307\u6811\u4e2d\u4efb\u610f\u4e24\u4e2a\u8282\u70b9\u4e4b\u95f4\u6700\u957f\u8def\u5f84\u7684&nbsp;<strong>\u957f\u5ea6<\/strong>&nbsp;\u3002\u8fd9\u6761\u8def\u5f84\u53ef\u80fd\u7ecf\u8fc7\u4e5f\u53ef\u80fd\u4e0d\u7ecf\u8fc7\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u4e24\u8282\u70b9\u4e4b\u95f4\u8def\u5f84\u7684&nbsp;<strong>\u957f\u5ea6<\/strong>&nbsp;\u7531\u5b83\u4eec\u4e4b\u95f4\u8fb9\u6570\u8868\u793a\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/06\/diamtree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [1,2,3,4,5]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>3<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>3 \uff0c\u53d6\u8def\u5f84 [4,2,1,3] \u6216 [5,2,1,3] \u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u9012\u5f52\uff0c\u5728\u6bcf\u4e00\u4e2a\u8282\u70b9\u62c6\u5206\u5904\u8ba1\u7b97\u4e00\u6b21\u6781\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def diameterOfBinaryTree(self, root: Optional&#91;TreeNode]) -&gt; int:\n        ans=0\n        def dfs(node:Optional&#91;TreeNode])-&gt;int:\n            if node==None:\n                return -1\n            l_len=dfs(node.left)+1\n            r_len=dfs(node.right)+1\n            nonlocal ans\n            ans=max(ans,l_len+r_len)\n            return max(l_len,r_len)\n        dfs(root)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">41.<a href=\"https:\/\/leetcode.cn\/problems\/binary-tree-level-order-traversal\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684&nbsp;<strong>\u5c42\u5e8f\u904d\u5386<\/strong>&nbsp;\u3002 \uff08\u5373\u9010\u5c42\u5730\uff0c\u4ece\u5de6\u5230\u53f3\u8bbf\u95ee\u6240\u6709\u8282\u70b9\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/19\/tree1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [3,9,20,null,null,15,7]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[3],[9,20],[15,7]]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u6784\u9020\u4e00\u4e2a\u961f\u5217\u5b58\u50a8\u6bcf\u884c\u8282\u70b9\uff0c\u904d\u5386\u6bcf\u884c\u524d\u5148\u8ba1\u7b97\u5f53\u524d\u884c\u6709\u591a\u5c11\u8282\u70b9\u9700\u8981\u904d\u5386\uff0c\u904d\u5386\u8282\u70b9\u65f6\u5c06\u4e0b\u4e00\u884c\u8282\u70b9\u4ece\u5de6\u5230\u53f3\u52a0\u5165\u961f\u5217\uff0c\u904d\u5386\u8fc7\u7684\u8282\u70b9\u51fa\u961f\u5217\uff0c\u76f4\u81f3\u961f\u5217\u4e3a\u7a7a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def levelOrder(self, root: Optional&#91;TreeNode]) -&gt; List&#91;List&#91;int]]:\n        if root==None:\n            return &#91;]\n        ans=&#91;]\n        temp=deque(&#91;root])\n        while temp:\n            vals=&#91;]\n            for i in range(len(temp)):\n                node=temp.popleft()\n                vals.append(node.val)\n                if node.left:\n                    temp.append(node.left)\n                if node.right:\n                    temp.append(node.right)\n            ans.append(vals)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">42.<a href=\"https:\/\/leetcode.cn\/problems\/convert-sorted-array-to-binary-search-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u5c06\u6709\u5e8f\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u5176\u4e2d\u5143\u7d20\u5df2\u7ecf\u6309&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u6392\u5217\uff0c\u8bf7\u4f60\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u68f5&nbsp;\u5e73\u8861&nbsp;\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/18\/btree1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [-10,-3,0,5,9]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[0,-3,9,-10,null,5]<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>[0,-10,5,null,-3,null,9] \u4e5f\u5c06\u88ab\u89c6\u4e3a\u6b63\u786e\u7b54\u6848\uff1a<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u9012\u5f52\uff0c\u6bcf\u6b21\u5c06\u6570\u7ec4\u62c6\u5206\u4e3a\u4e09\u90e8\u5206\uff0c\u5de6\u90e8\u5206\uff0c\u4e2d\u95f4\u6570\uff0c\u53f3\u90e8\u5206\uff0c\u6bcf\u6b21\u5c06\u4e2d\u95f4\u6570\u6784\u9020\u8282\u70b9\uff0c\u5e76\u4e3a\u5176\u6307\u5411\u5de6\u8282\u70b9\u548c\u53f3\u8282\u70b9\uff08\u9012\u5f52\u8fd4\u56de\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def sortedArrayToBST(self, nums: List&#91;int]) -&gt; Optional&#91;TreeNode]:\n        if not nums:\n            return None\n        m=len(nums)\/\/2\n        left=self.sortedArrayToBST(nums&#91;:m])\n        right=self.sortedArrayToBST(nums&#91;m+1:])\n        return TreeNode(nums&#91;m],left,right)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">43.<a href=\"https:\/\/leetcode.cn\/problems\/validate-binary-search-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u9a8c\u8bc1\u4e8c\u53c9\u641c\u7d22\u6811<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u5224\u65ad\u5176\u662f\u5426\u662f\u4e00\u4e2a\u6709\u6548\u7684\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/p>\n\n\n\n<p><strong>\u6709\u6548<\/strong>&nbsp;\u4e8c\u53c9\u641c\u7d22\u6811\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8282\u70b9\u7684\u5de6\u5b50\u6811\u53ea\u5305\u542b<strong>&nbsp;\u4e25\u683c\u5c0f\u4e8e&nbsp;<\/strong>\u5f53\u524d\u8282\u70b9\u7684\u6570\u3002<\/li>\n\n\n\n<li>\u8282\u70b9\u7684\u53f3\u5b50\u6811\u53ea\u5305\u542b&nbsp;<strong>\u4e25\u683c\u5927\u4e8e<\/strong>&nbsp;\u5f53\u524d\u8282\u70b9\u7684\u6570\u3002<\/li>\n\n\n\n<li>\u6240\u6709\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u81ea\u8eab\u5fc5\u987b\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/12\/01\/tree1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [2,1,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3<\/h2>\n\n\n\n<p>\u4e2d\u5e8f\u904d\u5386\u9012\u5f52\uff0c\u5de6\u4e2d\u53f3\uff0c\u7ed3\u5408\u4e8c\u53c9\u641c\u7d22\u6811\u7279\u6027\uff0c\u53ef\u77e5\u65b0\u8282\u70b9\u5e94\u5927\u4e8e\u6240\u6709\u524d\u7f6e\u8282\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    pre=-inf\n    def isValidBST(self, root: Optional&#91;TreeNode]) -&gt; bool:\n        if root is None:\n            return True\n        if not self.isValidBST(root.left):\n            return False\n        if root.val&lt;=self.pre:\n            return False\n        self.pre=root.val\n        return self.isValidBST(root.right)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">44.<a href=\"https:\/\/leetcode.cn\/problems\/kth-smallest-element-in-a-bst\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u7b2c K \u5c0f\u7684\u5143\u7d20<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u548c\u4e00\u4e2a\u6574\u6570&nbsp;<code>k<\/code>&nbsp;\uff0c\u8bf7\u4f60\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u67e5\u627e\u5176\u4e2d\u7b2c&nbsp;<code>k<\/code><strong>&nbsp;<\/strong>\u5c0f\u7684\u5143\u7d20\uff08<code>k<\/code>&nbsp;\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/28\/kthtree1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [3,1,4,null,2], k = 1<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>1<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\u7279\u6027\uff0c\u4e2d\u5e8f\u904d\u5386\u7ed3\u679c\u4e3a\u4ece\u5c0f\u5230\u5927\uff0c\u4e2d\u5e8f\u9012\u5f52\u8ba1\u6570\u5e76\u8bb0\u5f55\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def kthSmallest(self, root: Optional&#91;TreeNode], k: int) -&gt; int:\n        ans=0\n        def dfs(node: Optional&#91;TreeNode])-&gt;None:\n\n            nonlocal k,ans\n            if not node or k&lt;=0:\n                return\n            dfs(node.left)\n            k -=1\n            if k==0:\n                ans=node.val\n            dfs(node.right)\n\n        dfs(root)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">45.<a href=\"https:\/\/leetcode.cn\/problems\/binary-tree-right-side-view\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u7684\u53f3\u89c6\u56fe<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684&nbsp;<strong>\u6839\u8282\u70b9<\/strong>&nbsp;<code>root<\/code>\uff0c\u60f3\u8c61\u81ea\u5df1\u7ad9\u5728\u5b83\u7684\u53f3\u4fa7\uff0c\u6309\u7167\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u7684\u987a\u5e8f\uff0c\u8fd4\u56de\u4ece\u53f3\u4fa7\u6240\u80fd\u770b\u5230\u7684\u8282\u70b9\u503c\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>root = [1,2,3,null,5,null,4]<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>[1,3,4]<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2024\/11\/24\/tmpd5jn43fs-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u5c42\u5e8f\u904d\u5386\uff0c\u53d6\u6bcf\u5c42\u6700\u53f3\u4fa7\u8bb0\u5f55<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def rightSideView(self, root: Optional&#91;TreeNode]) -&gt; List&#91;int]:\n        q=deque(&#91;root])\n        ans=&#91;]\n        if not root:\n            return ans\n        while q:\n            n=len(q)\n            ans.append(q&#91;n-1].val)\n            for i in range(n):\n                node=q.popleft()\n                if node.left:\n                    q.append(node.left)\n                if node.right:\n                    q.append(node.right)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">46.<a href=\"https:\/\/leetcode.cn\/problems\/flatten-binary-tree-to-linked-list\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u5c55\u5f00\u4e3a\u94fe\u8868<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e8c\u53c9\u6811\u7684\u6839\u7ed3\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u8bf7\u4f60\u5c06\u5b83\u5c55\u5f00\u4e3a\u4e00\u4e2a\u5355\u94fe\u8868\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c55\u5f00\u540e\u7684\u5355\u94fe\u8868\u5e94\u8be5\u540c\u6837\u4f7f\u7528&nbsp;<code>TreeNode<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>right<\/code>&nbsp;\u5b50\u6307\u9488\u6307\u5411\u94fe\u8868\u4e2d\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff0c\u800c\u5de6\u5b50\u6307\u9488\u59cb\u7ec8\u4e3a&nbsp;<code>null<\/code>&nbsp;\u3002<\/li>\n\n\n\n<li>\u5c55\u5f00\u540e\u7684\u5355\u94fe\u8868\u5e94\u8be5\u4e0e\u4e8c\u53c9\u6811&nbsp;<a href=\"https:\/\/baike.baidu.com\/item\/%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86\/6442839?fr=aladdin\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" ><strong>\u5148\u5e8f\u904d\u5386<\/strong><\/a>&nbsp;\u987a\u5e8f\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/01\/14\/flaten.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [1,2,5,3,4,null,6]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,null,2,null,3,null,4,null,5,null,6]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u524d\u5e8f\u904d\u5386\uff1a\u4e2d\u5de6\u53f3\uff0c\u82e5\u4e00\u8fb9\u904d\u5386\u4e00\u904d\u6784\u9020\u65b0\u94fe\u8868\uff0c\u5219\u4f1a\u7834\u73af\u6811\u5bfc\u81f4\u5931\u8d25\u3002<br>\u540e\u5e8f\u904d\u5386\uff1a\u53f3\u5de6\u4e2d\uff0c\u987a\u5e8f\u4e0e\u524d\u5e8f\u76f8\u53cd\uff0c\u4e14\u904d\u5386\u4ece\u53f6\u5b50\u8282\u70b9\u5f00\u59cb\uff0c\u53ef\u4ee5\u4e00\u8fb9\u904d\u5386\u4e00\u8fb9\u62c6\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    head=None\n    def flatten(self, root: Optional&#91;TreeNode]) -&gt; None:\n        \"\"\"\n        Do not return anything, modify root in-place instead.\n        \"\"\"\n        \n        if not root:\n            return \n        self.flatten(root.right)\n        self.flatten(root.left)\n        root.left=None\n        root.right=self.head\n        self.head=root<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">47.<a href=\"https:\/\/leetcode.cn\/problems\/construct-binary-tree-from-preorder-and-inorder-traversal\/\" target=\"_blank\"  rel=\"nofollow\" >\u4ece\u524d\u5e8f\u4e0e\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u6784\u9020\u4e8c\u53c9\u6811<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>preorder<\/code>&nbsp;\u548c&nbsp;<code>inorder<\/code>&nbsp;\uff0c\u5176\u4e2d&nbsp;<code>preorder<\/code>&nbsp;\u662f\u4e8c\u53c9\u6811\u7684<strong>\u5148\u5e8f\u904d\u5386<\/strong>\uff0c&nbsp;<code>inorder<\/code>&nbsp;\u662f\u540c\u4e00\u68f5\u6811\u7684<strong>\u4e2d\u5e8f\u904d\u5386<\/strong>\uff0c\u8bf7\u6784\u9020\u4e8c\u53c9\u6811\u5e76\u8fd4\u56de\u5176\u6839\u8282\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/19\/tree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165<\/strong><strong>:<\/strong> preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa:<\/strong> [3,9,20,null,null,15,7]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u524d\u5e8f\uff1a \u4e2d\u5de6\u53f3<br>\u4e2d\uff08xxxxxxxxx\u5de6\uff09\uff08xxxxxxxxx\u53f3\uff09<\/p>\n\n\n\n<p>\u4e2d\u5e8f\uff1a\u5de6\u4e2d\u53f3<br>\uff08xxxxxxxxx\u5de6\uff09\u4e2d\uff08xxxxxxxxx\u53f3\uff09<\/p>\n\n\n\n<p>\u5728\u4e2d\u5e8f\u5e8f\u5217\u627e\u5230\u524d\u5e8f[0]\u7684index\uff0c\u5f97\u5230\u5de6\u5b50\u6811\u957f\u5ea6\uff0c\u6839\u636e\u957f\u5ea6\u5212\u5206\u4e3a\u5de6\u4e2d\u53f3\u4e09\u90e8\u5206\uff0c\u5de6\u548c\u53f3\u5206\u522b\u9012\u5f52\uff0c\u4e2d\u6784\u9020\u6811\u8282\u70b9\uff0c\u5de6\u53f3\u5b50\u6811\u5206\u522b\u6307\u5411\u9012\u5f52\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def buildTree(self, preorder: List&#91;int], inorder: List&#91;int]) -&gt; Optional&#91;TreeNode]:\n        if not preorder:\n            return None\n        left_size =inorder.index(preorder&#91;0])\n        left=self.buildTree(preorder&#91;1:1+left_size],inorder&#91;:left_size])\n        right=self.buildTree(preorder&#91;1+left_size:],inorder&#91;left_size+1:])\n        return TreeNode(preorder&#91;0],left,right)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">48.<a href=\"https:\/\/leetcode.cn\/problems\/path-sum-iii\/\" target=\"_blank\"  rel=\"nofollow\" >\u8def\u5f84\u603b\u548c III<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u548c\u4e00\u4e2a\u6574\u6570&nbsp;<code>targetSum<\/code>&nbsp;\uff0c\u6c42\u8be5\u4e8c\u53c9\u6811\u91cc\u8282\u70b9\u503c\u4e4b\u548c\u7b49\u4e8e&nbsp;<code>targetSum<\/code>&nbsp;\u7684&nbsp;<strong>\u8def\u5f84<\/strong>&nbsp;\u7684\u6570\u76ee\u3002<\/p>\n\n\n\n<p><strong>\u8def\u5f84<\/strong>&nbsp;\u4e0d\u9700\u8981\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4e5f\u4e0d\u9700\u8981\u5728\u53f6\u5b50\u8282\u70b9\u7ed3\u675f\uff0c\u4f46\u662f\u8def\u5f84\u65b9\u5411\u5fc5\u987b\u662f\u5411\u4e0b\u7684\uff08\u53ea\u80fd\u4ece\u7236\u8282\u70b9\u5230\u5b50\u8282\u70b9\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/04\/09\/pathsum3-1-tree.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>3<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u548c\u7b49\u4e8e 8 \u7684\u8def\u5f84\u6709 3 \u6761\uff0c\u5982\u56fe\u6240\u793a\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u4e2d\u5e8f\u904d\u5386\uff0c\u5e76\u7528\u5b57\u5178\u8bb0\u5f55\u8def\u5f84\u4e0a\u7684\u6240\u6709\u548c\u7684\u4e2a\u6570\uff0c\u5f53s-target\u5728pre\u8bb0\u5f55\u4e2d\u53ef\u67e5\u5230\u65f6\uff0cans\u52a0\u4e0a\u8fd9\u4e2a\u503c\uff0c\u904d\u5386\u5230\u7ec8\u70b9\u56de\u6eaf\u65f6\u5c06pre\u4e2d\u7684s\u8bb0\u5f55\u51cf1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def pathSum(self, root: Optional&#91;TreeNode], targetSum: int) -&gt; int:\n        ans = 0\n        pre = defaultdict(int)\n        pre&#91;0] = 1\n        def dfs(node: Optional&#91;TreeNode],s:int) -&gt; None:\n            nonlocal ans\n            # \u5f53\u524d\u8282\u70b9\u4e3a\u7a7a\u5219\u8fd4\u56de\n            if not node:\n                return\n            s+=node.val\n            ans +=pre&#91;s-targetSum]\n            pre&#91;s]+=1\n            dfs(node.left,s)\n            dfs(node.right,s)\n            pre&#91;s]-=1          \n        \n        dfs(root,0)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">49.<a href=\"https:\/\/leetcode.cn\/problems\/lowest-common-ancestor-of-a-binary-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811, \u627e\u5230\u8be5\u6811\u4e2d\u4e24\u4e2a\u6307\u5b9a\u8282\u70b9\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/baike.baidu.com\/item\/%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88\/8918834?fr=aladdin\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u767e\u5ea6\u767e\u79d1<\/a>\u4e2d\u6700\u8fd1\u516c\u5171\u7956\u5148\u7684\u5b9a\u4e49\u4e3a\uff1a\u201c\u5bf9\u4e8e\u6709\u6839\u6811 T \u7684\u4e24\u4e2a\u8282\u70b9 p\u3001q\uff0c\u6700\u8fd1\u516c\u5171\u7956\u5148\u8868\u793a\u4e3a\u4e00\u4e2a\u8282\u70b9 x\uff0c\u6ee1\u8db3 x \u662f p\u3001q \u7684\u7956\u5148\u4e14 x \u7684\u6df1\u5ea6\u5c3d\u53ef\u80fd\u5927\uff08<strong>\u4e00\u4e2a\u8282\u70b9\u4e5f\u53ef\u4ee5\u662f\u5b83\u81ea\u5df1\u7684\u7956\u5148<\/strong>\uff09\u3002\u201d<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/14\/binarytree.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>3<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u8282\u70b9 <code>5 <\/code>\u548c\u8282\u70b9 <code>1 <\/code>\u7684\u6700\u8fd1\u516c\u5171\u7956\u5148\u662f\u8282\u70b9 <code>3 \u3002<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u524d\u5e8f\u904d\u5386\uff0c\u4e2d\u5de6\u53f3\uff0croot\u4e3a\u76ee\u6807\u8282\u70b9\u5373\u8fd4\u56de\uff0c\u4e0d\u518d\u5411\u4e0b\u9012\u5f52\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -&gt; 'TreeNode':\n        if root in (None, p, q):  # \u627e\u5230 p \u6216 q \u5c31\u4e0d\u5f80\u4e0b\u9012\u5f52\u4e86\uff0c\u539f\u56e0\u89c1\u4e0a\u9762\u7b54\u7591\n            return root\n        left = self.lowestCommonAncestor(root.left, p, q)\n        right = self.lowestCommonAncestor(root.right, p, q)\n        if left and right:  # \u5de6\u53f3\u90fd\u627e\u5230\n            return root  # \u5f53\u524d\u8282\u70b9\u662f\u6700\u8fd1\u516c\u5171\u7956\u5148\n        # \u5982\u679c\u53ea\u6709\u5de6\u5b50\u6811\u627e\u5230\uff0c\u5c31\u8fd4\u56de\u5de6\u5b50\u6811\u7684\u8fd4\u56de\u503c\n        # \u5982\u679c\u53ea\u6709\u53f3\u5b50\u6811\u627e\u5230\uff0c\u5c31\u8fd4\u56de\u53f3\u5b50\u6811\u7684\u8fd4\u56de\u503c\n        # \u5982\u679c\u5de6\u53f3\u5b50\u6811\u90fd\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u8fd4\u56de None\uff08\u6ce8\u610f\u6b64\u65f6 right = None\uff09\n        return left or right<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">50.<a href=\"https:\/\/leetcode.cn\/problems\/binary-tree-maximum-path-sum\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e8c\u53c9\u6811\u4e2d\u7684\u6700\u5927\u8def\u5f84\u548c<\/a><\/h2>\n\n\n\n<p>\u4e8c\u53c9\u6811\u4e2d\u7684<strong>&nbsp;\u8def\u5f84<\/strong>&nbsp;\u88ab\u5b9a\u4e49\u4e3a\u4e00\u6761\u8282\u70b9\u5e8f\u5217\uff0c\u5e8f\u5217\u4e2d\u6bcf\u5bf9\u76f8\u90bb\u8282\u70b9\u4e4b\u95f4\u90fd\u5b58\u5728\u4e00\u6761\u8fb9\u3002\u540c\u4e00\u4e2a\u8282\u70b9\u5728\u4e00\u6761\u8def\u5f84\u5e8f\u5217\u4e2d&nbsp;<strong>\u81f3\u591a\u51fa\u73b0\u4e00\u6b21<\/strong>&nbsp;\u3002\u8be5\u8def\u5f84<strong>&nbsp;\u81f3\u5c11\u5305\u542b\u4e00\u4e2a&nbsp;<\/strong>\u8282\u70b9\uff0c\u4e14\u4e0d\u4e00\u5b9a\u7ecf\u8fc7\u6839\u8282\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u8def\u5f84\u548c<\/strong>&nbsp;\u662f\u8def\u5f84\u4e2d\u5404\u8282\u70b9\u503c\u7684\u603b\u548c\u3002<\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u8fd4\u56de\u5176&nbsp;<strong>\u6700\u5927\u8def\u5f84\u548c<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/13\/exx1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>root = [1,2,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>6<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u6700\u4f18\u8def\u5f84\u662f 2 -&gt; 1 -&gt; 3 \uff0c\u8def\u5f84\u548c\u4e3a 2 + 1 + 3 = 6<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u540e\u7eed\u904d\u5386\u9012\u5f52\uff0c\u67d0\u8282\u70b9\u7684\u6700\u5927\u503c\u4e3a\u5176\u5de6\u8282\u70b9\u9012\u5f52\u7ed3\u679c\u52a0\u4e0a\u53f3\u8282\u70b9\u9012\u5f52\u7ed3\u679c\uff0c\u67d0\u5206\u652f\u7ed3\u679c\u5c0f\u4e8e0\u5219\u76f4\u63a5\u820d\u53bb\uff0c\u53d60<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxPathSum(self, root: Optional&#91;TreeNode]) -&gt; int:\n        ans = -inf\n        def dfs(node: Optional&#91;TreeNode]) -&gt; int:\n            if node is None:\n                return 0  # \u6ca1\u6709\u8282\u70b9\uff0c\u548c\u4e3a 0\n            l_val = dfs(node.left)  # \u5de6\u5b50\u6811\u6700\u5927\u94fe\u548c\n            r_val = dfs(node.right)  # \u53f3\u5b50\u6811\u6700\u5927\u94fe\u548c\n            nonlocal ans\n            ans = max(ans, l_val + r_val + node.val)  # \u4e24\u6761\u94fe\u62fc\u6210\u8def\u5f84\n            return max(max(l_val, r_val) + node.val, 0)  # \u5f53\u524d\u5b50\u6811\u6700\u5927\u94fe\u548c\uff08\u6ce8\u610f\u8fd9\u91cc\u548c 0 \u53d6\u6700\u5927\u503c\u4e86\uff09\n        dfs(root)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">51.<a href=\"https:\/\/leetcode.cn\/problems\/number-of-islands\/\" target=\"_blank\"  rel=\"nofollow\" >\u5c9b\u5c7f\u6570\u91cf<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u7531&nbsp;<code>'1'<\/code>\uff08\u9646\u5730\uff09\u548c&nbsp;<code>'0'<\/code>\uff08\u6c34\uff09\u7ec4\u6210\u7684\u7684\u4e8c\u7ef4\u7f51\u683c\uff0c\u8bf7\u4f60\u8ba1\u7b97\u7f51\u683c\u4e2d\u5c9b\u5c7f\u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<p>\u5c9b\u5c7f\u603b\u662f\u88ab\u6c34\u5305\u56f4\uff0c\u5e76\u4e14\u6bcf\u5ea7\u5c9b\u5c7f\u53ea\u80fd\u7531\u6c34\u5e73\u65b9\u5411\u548c\/\u6216\u7ad6\u76f4\u65b9\u5411\u4e0a\u76f8\u90bb\u7684\u9646\u5730\u8fde\u63a5\u5f62\u6210\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u5047\u8bbe\u8be5\u7f51\u683c\u7684\u56db\u6761\u8fb9\u5747\u88ab\u6c34\u5305\u56f4\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>grid = [<\/p>\n\n\n\n<p>&nbsp; ['1','1','1','1','0'],<\/p>\n\n\n\n<p>&nbsp; ['1','1','0','1','0'],<\/p>\n\n\n\n<p>&nbsp; ['1','1','0','0','0'],<\/p>\n\n\n\n<p>&nbsp; ['0','0','0','0','0']<\/p>\n\n\n\n<p>]<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u5c06\u76f8\u8fde\u76841\u5168\u90e8\u7f6e\u4e3a2\uff08\u9012\u5f52\uff09\uff0c\u904d\u5386\u6570\u7ec4\u5bfb\u627e1\uff0c\u627e\u5230\u4e00\u4e2a1\u540e\u542f\u52a8dfs,\u5e76\u5c9b\u5c7f\u8ba1\u6570\u52a01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def numIslands(self, grid: List&#91;List&#91;str]]) -&gt; int:\n        row_n=len(grid)\n        col_n=len(grid&#91;0])\n        def dfs(row:int,col:int)-&gt;None:\n            if row &lt;0 or row&gt;=row_n or col&lt;0 or col&gt;=col_n or grid&#91;row]&#91;col]!='1' :\n                return\n            grid&#91;row]&#91;col]='2'\n            dfs(row,col-1)\n            dfs(row,col+1)\n            dfs(row-1,col)\n            dfs(row+1,col)\n        ans=0\n        for row,line in enumerate(grid):\n            for col,num in enumerate(line):\n                if num =='1':\n                    dfs(row,col)\n                    ans+=1\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">52.<a href=\"https:\/\/leetcode.cn\/problems\/rotting-oranges\/\" target=\"_blank\"  rel=\"nofollow\" >\u8150\u70c2\u7684\u6a58\u5b50<\/a><\/h2>\n\n\n\n<p>\u5728\u7ed9\u5b9a\u7684&nbsp;<code>m x n<\/code>&nbsp;\u7f51\u683c&nbsp;<code>grid<\/code>&nbsp;\u4e2d\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e09\u4e2a\u503c\u4e4b\u4e00\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u503c&nbsp;<code>0<\/code>&nbsp;\u4ee3\u8868\u7a7a\u5355\u5143\u683c\uff1b<\/li>\n\n\n\n<li>\u503c&nbsp;<code>1<\/code>&nbsp;\u4ee3\u8868\u65b0\u9c9c\u6a58\u5b50\uff1b<\/li>\n\n\n\n<li>\u503c&nbsp;<code>2<\/code>&nbsp;\u4ee3\u8868\u8150\u70c2\u7684\u6a58\u5b50\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6bcf\u5206\u949f\uff0c\u8150\u70c2\u7684\u6a58\u5b50&nbsp;<strong>\u5468\u56f4&nbsp;4 \u4e2a\u65b9\u5411\u4e0a\u76f8\u90bb<\/strong>&nbsp;\u7684\u65b0\u9c9c\u6a58\u5b50\u90fd\u4f1a\u8150\u70c2\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de&nbsp;<em>\u76f4\u5230\u5355\u5143\u683c\u4e2d\u6ca1\u6709\u65b0\u9c9c\u6a58\u5b50\u4e3a\u6b62\u6240\u5fc5\u987b\u7ecf\u8fc7\u7684\u6700\u5c0f\u5206\u949f\u6570\u3002\u5982\u679c\u4e0d\u53ef\u80fd\uff0c\u8fd4\u56de&nbsp;<code>-1<\/code><\/em>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2019\/02\/16\/oranges.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>grid = [[2,1,1],[1,1,0],[0,1,1]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>4<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>\u8bb0\u5f55\u8150\u70c2\u6c34\u679c\u7684\u5750\u6807\uff0c\u5b58\u5165\u6570\u7ec4\uff0c\u8bb0\u5f55\u65b0\u9c9c\u6c34\u679c\u7684\u4e2a\u6570<\/p>\n\n\n\n<p>\u904d\u5386\u8150\u70c2\u6c34\u679c\u7684\u5750\u6807\uff0c\u5224\u65ad\u5176\u56db\u4e2a\u65b9\u4f4d\u7684\u6c34\u679c\u662f\u5426\u65b0\u9c9c\uff0c\u662f\u5219\u5c06\u5176\u52a0\u5165\u8150\u70c2\u6c34\u679c\u7684\u5750\u6807\uff0c\u65e7\u5750\u6807\u6e05\u7a7a\uff0c\u65b0\u9c9c\u6c34\u679c\u8ba1\u6570\u51cf1\uff0c\u65f6\u95f4\u8ba1\u6570\u6bcf\u8f6e\u52a01<\/p>\n\n\n\n<p>\u5f53\u8150\u70c2\u6c34\u679c\u5750\u6807\u6570\u7ec4\u4e3a\u7a7a\u6216\u65b0\u9c9c\u6c34\u679c\u6570\u4e3a0\uff0c\u5faa\u73af\u7ed3\u675f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def orangesRotting(self, grid: List&#91;List&#91;int]]) -&gt; int:\n        r=&#91;]\n        f=0\n        ans=0\n        m,n=len(grid),len(grid&#91;0])\n        for row,line in enumerate(grid):\n            for col,num in enumerate(line):\n                if num==2:\n                    r.append((row,col))\n                elif num==1:\n                    f+=1\n        while r and f:\n            tmp=r\n            ans+=1\n            r=&#91;]\n            for row,col in tmp:\n                for row_,col_ in (row-1,col),(row+1,col),(row,col-1),(row,col+1):\n                    if 0&lt;=row_&lt;m and 0&lt;=col_&lt;n and grid&#91;row_]&#91;col_]==1:\n                        f-=1\n                        grid&#91;row_]&#91;col_]=2\n                        r.append((row_,col_))\n        return -1 if f else ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">53.<a href=\"https:\/\/leetcode.cn\/problems\/course-schedule\/\" target=\"_blank\"  rel=\"nofollow\" >\u8bfe\u7a0b\u8868<\/a><\/h2>\n\n\n\n<p>\u4f60\u8fd9\u4e2a\u5b66\u671f\u5fc5\u987b\u9009\u4fee&nbsp;<code>numCourses<\/code>&nbsp;\u95e8\u8bfe\u7a0b\uff0c\u8bb0\u4e3a&nbsp;<code>0<\/code>&nbsp;\u5230&nbsp;<code>numCourses - 1<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u5728\u9009\u4fee\u67d0\u4e9b\u8bfe\u7a0b\u4e4b\u524d\u9700\u8981\u4e00\u4e9b\u5148\u4fee\u8bfe\u7a0b\u3002 \u5148\u4fee\u8bfe\u7a0b\u6309\u6570\u7ec4&nbsp;<code>prerequisites<\/code>&nbsp;\u7ed9\u51fa\uff0c\u5176\u4e2d&nbsp;<code>prerequisites[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>&nbsp;\uff0c\u8868\u793a\u5982\u679c\u8981\u5b66\u4e60\u8bfe\u7a0b&nbsp;<code>a<sub>i<\/sub><\/code>&nbsp;\u5219&nbsp;<strong>\u5fc5\u987b<\/strong>&nbsp;\u5148\u5b66\u4e60\u8bfe\u7a0b&nbsp;&nbsp;<code>b<sub>i<\/sub><\/code><sub>&nbsp;<\/sub>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u5982\uff0c\u5148\u4fee\u8bfe\u7a0b\u5bf9&nbsp;<code>[0, 1]<\/code>&nbsp;\u8868\u793a\uff1a\u60f3\u8981\u5b66\u4e60\u8bfe\u7a0b&nbsp;<code>0<\/code>&nbsp;\uff0c\u4f60\u9700\u8981\u5148\u5b8c\u6210\u8bfe\u7a0b&nbsp;<code>1<\/code>&nbsp;\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8bf7\u4f60\u5224\u65ad\u662f\u5426\u53ef\u80fd\u5b8c\u6210\u6240\u6709\u8bfe\u7a0b\u7684\u5b66\u4e60\uff1f\u5982\u679c\u53ef\u4ee5\uff0c\u8fd4\u56de&nbsp;<code>true<\/code>&nbsp;\uff1b\u5426\u5219\uff0c\u8fd4\u56de&nbsp;<code>false<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>numCourses = 2, prerequisites = [[1,0]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u603b\u5171\u6709 2 \u95e8\u8bfe\u7a0b\u3002\u5b66\u4e60\u8bfe\u7a0b 1 \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5b8c\u6210\u8bfe\u7a0b 0 \u3002\u8fd9\u662f\u53ef\u80fd\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\uff1a<\/h2>\n\n\n\n<p>g=[[] for _ in range(numCourses)\u521d\u59cb\u5316\u7a7a\u90bb\u63a5\u8868\uff0cg[\u5f85\u4fee\u8bfe\u7a0b].append(\u524d\u7f6e\u8bfe\u7a0b)<\/p>\n\n\n\n<p>\u4f9d\u6b21\u904d\u5386\u6240\u6709\u8bfe\u7a0b\uff0c\u9012\u5f52\u904d\u5386\u5176\u90bb\u63a5\u5217\u8868\uff0cdfs\u904d\u5386\u5230\u6807\u8bb0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def canFinish(self, numCourses: int, prerequisites: List&#91;List&#91;int]]) -&gt; bool:\n        g = &#91;&#91;] for _ in range(numCourses)]\n        for a, b in prerequisites:\n            g&#91;b].append(a)\n\n        colors = &#91;0] * numCourses\n        # \u8fd4\u56de True \u8868\u793a\u627e\u5230\u4e86\u73af\n        def dfs(x: int) -&gt; bool:\n            colors&#91;x] = 1  # x \u6b63\u5728\u8bbf\u95ee\u4e2d\n            for y in g&#91;x]:\n                # \u60c5\u51b5\u4e00\uff1acolors&#91;y] == 1\uff0c\u8868\u793a\u53d1\u751f\u5faa\u73af\u4f9d\u8d56\uff0c\u627e\u5230\u4e86\u73af\n                # \u60c5\u51b5\u4e8c\uff1acolors&#91;y] == 0\uff0c\u6ca1\u6709\u8bbf\u95ee\u8fc7 y\uff0c\u7ee7\u7eed\u9012\u5f52 y \u83b7\u53d6\u4fe1\u606f\n                # \u60c5\u51b5\u4e09\uff1acolors&#91;y] == 2\uff0c\u91cd\u590d\u8bbf\u95ee y \u53ea\u4f1a\u91cd\u8e48\u8986\u8f99\uff0c\u548c\u4e4b\u524d\u4e00\u6837\u65e0\u6cd5\u627e\u5230\u73af\uff0c\u8df3\u8fc7\n                if colors&#91;y] == 1 or colors&#91;y] == 0 and dfs(y):\n                    return True  # \u627e\u5230\u4e86\u73af\n            colors&#91;x] = 2  # x \u5b8c\u5168\u8bbf\u95ee\u5b8c\u6bd5\uff0c\u4ece x \u51fa\u53d1\u65e0\u6cd5\u627e\u5230\u73af\n            return False  # \u6ca1\u6709\u627e\u5230\u73af\n\n        for i, c in enumerate(colors):\n            if c == 0 and dfs(i):\n                return False  # \u6709\u73af\n        return True  # \u6ca1\u6709\u73af<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">54.<a href=\"https:\/\/leetcode.cn\/problems\/permutations\/\" target=\"_blank\"  rel=\"nofollow\" >\u5168\u6392\u5217<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e0d\u542b\u91cd\u590d\u6570\u5b57\u7684\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u8fd4\u56de\u5176&nbsp;<em>\u6240\u6709\u53ef\u80fd\u7684\u5168\u6392\u5217<\/em>&nbsp;\u3002\u4f60\u53ef\u4ee5&nbsp;<strong>\u6309\u4efb\u610f\u987a\u5e8f<\/strong>&nbsp;\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,2,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u5b9a\u4e49\u4e00\u4e2a\u6807\u8bb0\u6570\u7ec4\uff0c\u5df2\u52a0\u5165\u7ed3\u679c\u7684\u6807\u8bb0\u4e3aTrue\uff0c\u9012\u5f52\u904d\u5386\uff0c\u76f4\u5230\u8ba1\u6570n\u7b49\u4e8e\u5217\u8868\u957f\uff0c\u52a0\u5165\u7ed3\u679c\u5e76\u56de\u6eaf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def permute(self, nums: List&#91;int]) -&gt; List&#91;List&#91;int]]:\n        l=len(nums)\n        ans=&#91;]\n        flg_nums=&#91;False]*l\n        temp=&#91;]\n        def dfs(n:int)-&gt;None:\n            if n==l:\n                ans.append(temp&#91;:])\n                return\n            for index,flag in enumerate(flg_nums):\n                if not flag:\n                    flg_nums&#91;index]=True\n                    temp.append(nums&#91;index])\n                    dfs(n+1)\n                    flg_nums&#91;index]=False\n                    temp.pop()\n        dfs(0)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">55.<a href=\"https:\/\/leetcode.cn\/problems\/subsets\/\" target=\"_blank\"  rel=\"nofollow\" >\u5b50\u96c6<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20&nbsp;<strong>\u4e92\u4e0d\u76f8\u540c<\/strong>&nbsp;\u3002\u8fd4\u56de\u8be5\u6570\u7ec4\u6240\u6709\u53ef\u80fd\u7684\u5b50\u96c6\uff08\u5e42\u96c6\uff09\u3002<\/p>\n\n\n\n<p>\u89e3\u96c6&nbsp;<strong>\u4e0d\u80fd<\/strong>&nbsp;\u5305\u542b\u91cd\u590d\u7684\u5b50\u96c6\u3002\u4f60\u53ef\u4ee5\u6309&nbsp;<strong>\u4efb\u610f\u987a\u5e8f<\/strong>&nbsp;\u8fd4\u56de\u89e3\u96c6\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,2,3]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u9012\u5f52\uff0c\u6bcf\u6b21dfs\u65f6\u9009\u62e9\u53d6\u8fd9\u4e2a\u6570\u6216\u4e0d\u53d6\u8fd9\u4e2a\u6570\uff0c\u53d6\u6570\u540e\u56de\u6eaf\u65f6\u6ce8\u610f\u590d\u539f\u4e2d\u95f4\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def subsets(self, nums: List&#91;int]) -&gt; List&#91;List&#91;int]]:\n        n=len(nums)\n        ans=&#91;]\n        path=&#91;]\n\n        def dfs(i:int)-&gt;None:\n            if i==n:\n                ans.append(path&#91;:])\n                return\n            dfs(i+1)\n            path.append(nums&#91;i])\n            dfs(i+1)\n            path.pop()\n        dfs(0)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">56.<a href=\"https:\/\/leetcode.cn\/problems\/search-insert-position\/\" target=\"_blank\"  rel=\"nofollow\" >\u641c\u7d22\u63d2\u5165\u4f4d\u7f6e<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6392\u5e8f\u6570\u7ec4\u548c\u4e00\u4e2a\u76ee\u6807\u503c\uff0c\u5728\u6570\u7ec4\u4e2d\u627e\u5230\u76ee\u6807\u503c\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002\u5982\u679c\u76ee\u6807\u503c\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\uff0c\u8fd4\u56de\u5b83\u5c06\u4f1a\u88ab\u6309\u987a\u5e8f\u63d2\u5165\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u8bf7\u5fc5\u987b\u4f7f\u7528\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(log n)<\/code>&nbsp;\u7684\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165:<\/strong> nums = [1,3,5,6], target = 5<\/li>\n\n\n\n<li><strong>\u8f93\u51fa:<\/strong> 2<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u5178\u578b\u4e8c\u5206\u67e5\u627e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def searchInsert(self, nums: List&#91;int], target: int) -&gt; int:\n        l,r=0,len(nums)-1\n        while l&lt;=r:\n            mid =(l+r)\/\/2\n            if nums&#91;mid]&lt;target:\n                l=mid+1\n            else:\n                r=mid-1\n        return l<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">57.<a href=\"https:\/\/leetcode.cn\/problems\/search-a-2d-matrix\/\" target=\"_blank\"  rel=\"nofollow\" >&nbsp;\u641c\u7d22\u4e8c\u7ef4\u77e9\u9635<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6ee1\u8db3\u4e0b\u8ff0\u4e24\u6761\u5c5e\u6027\u7684&nbsp;<code>m x n<\/code>&nbsp;\u6574\u6570\u77e9\u9635\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u884c\u4e2d\u7684\u6574\u6570\u4ece\u5de6\u5230\u53f3\u6309\u975e\u4e25\u683c\u9012\u589e\u987a\u5e8f\u6392\u5217\u3002<\/li>\n\n\n\n<li>\u6bcf\u884c\u7684\u7b2c\u4e00\u4e2a\u6574\u6570\u5927\u4e8e\u524d\u4e00\u884c\u7684\u6700\u540e\u4e00\u4e2a\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570&nbsp;<code>target<\/code>&nbsp;\uff0c\u5982\u679c&nbsp;<code>target<\/code>&nbsp;\u5728\u77e9\u9635\u4e2d\uff0c\u8fd4\u56de&nbsp;<code>true<\/code>&nbsp;\uff1b\u5426\u5219\uff0c\u8fd4\u56de&nbsp;<code>false<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/05\/mat.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u5c06\u77e9\u9635\u5f53\u6210m*n\u957f\u5ea6\u7684\u5217\u8868\uff0c\u540c\u6837\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u4e2d\u95f4\u591a\u4e00\u6b65\u5750\u6807\u89e3\u6790<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def searchMatrix(self, matrix: List&#91;List&#91;int]], target: int) -&gt; bool:\n        row,col =len(matrix),len(matrix&#91;0])\n        l,r=0,row*col-1\n        while l&lt;=r:\n            mid =(l+r)\/\/2\n            mid_row=mid\/\/col\n            mid_col=mid%col\n            if matrix&#91;mid_row]&#91;mid_col]&lt;target:\n                l=mid+1\n            elif matrix&#91;mid_row]&#91;mid_col]&gt;target:\n                r=mid-1\n            else :\n                return True\n        return False<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">57.<a href=\"https:\/\/leetcode.cn\/problems\/find-first-and-last-position-of-element-in-sorted-array\/\" target=\"_blank\"  rel=\"nofollow\" >\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6309\u7167\u975e\u9012\u51cf\u987a\u5e8f\u6392\u5217\u7684\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u548c\u4e00\u4e2a\u76ee\u6807\u503c&nbsp;<code>target<\/code>\u3002\u8bf7\u4f60\u627e\u51fa\u7ed9\u5b9a\u76ee\u6807\u503c\u5728\u6570\u7ec4\u4e2d\u7684\u5f00\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u76ee\u6807\u503c&nbsp;<code>target<\/code>\uff0c\u8fd4\u56de&nbsp;<code>[-1, -1]<\/code>\u3002<\/p>\n\n\n\n<p>\u4f60\u5fc5\u987b\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(log n)<\/code>&nbsp;\u7684\u7b97\u6cd5\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [<code>5,7,7,8,8,10]<\/code>, target = 8<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[3,4]<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u4e8c\u5206\u67e5\u627e\uff0c\u5f53nums[mid]&lt;target\u65f6\uff0cl=mid+1,\u5176\u4ed6\u60c5\u51b5\u4e0bright\u5de6\u79fb\uff0c\u867d\u7136right\u4f1a\u79fb\u5230\u5c0f\u4e8etarget\u7684\u6570\u4e0a\uff0c\u4f46\u662f\u5f53l==r\u65f6\uff0c\u8fd9\u4e00\u8f6e\u5224\u65ad\u4f1a\u8ba9l\u53f3\u79fb\uff0c\u56e0\u6b64l\u4f1a\u6307\u5411\u7b2c\u4e00\u4e2atarget\u3002\u5728\u68c0\u7d22target+1\u7684\u4f4d\u7f6e\uff0c\u627e\u5230\u540e\u51cf\u4e00\u5373\u4e3atarget\u6700\u540e\u4e00\u4f4d\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def erfen(self, nums: List&#91;int], target: int)-&gt;int:\n        l,r=0,len(nums)-1\n        while l&lt;=r:\n            mid =(l+r)\/\/2\n            if nums&#91;mid]&lt;target:\n                l=mid+1\n            else:\n                r=mid-1\n        return l\n\n    def searchRange(self, nums: List&#91;int], target: int) -&gt; List&#91;int]:\n        left=self.erfen(nums,target)\n\/\/\u6ce8\u610f\u5148\u5224\u65adleft\u662f\u5426\u8d8a\u754c\uff0c\u518d\u5224\u65adnums&#91;left]\u503c\u5bf9\u4e0d\u5bf9\n        if left==len(nums) or nums&#91;left]!=target :\n            return&#91;-1,-1]\n        right=self.erfen(nums,target+1)-1\n        return &#91;left,right]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">58.<a href=\"https:\/\/leetcode.cn\/problems\/min-stack\/\" target=\"_blank\"  rel=\"nofollow\" >\u6700\u5c0f\u6808<\/a><\/h2>\n\n\n\n<p>\u8bbe\u8ba1\u4e00\u4e2a\u652f\u6301&nbsp;<code>push<\/code>&nbsp;\uff0c<code>pop<\/code>&nbsp;\uff0c<code>top<\/code>&nbsp;\u64cd\u4f5c\uff0c\u5e76\u80fd\u5728\u5e38\u6570\u65f6\u95f4\u5185\u68c0\u7d22\u5230\u6700\u5c0f\u5143\u7d20\u7684\u6808\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0&nbsp;<code>MinStack<\/code>&nbsp;\u7c7b:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MinStack()<\/code>&nbsp;\u521d\u59cb\u5316\u5806\u6808\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>void push(int val)<\/code>&nbsp;\u5c06\u5143\u7d20val\u63a8\u5165\u5806\u6808\u3002<\/li>\n\n\n\n<li><code>void pop()<\/code>&nbsp;\u5220\u9664\u5806\u6808\u9876\u90e8\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>int top()<\/code>&nbsp;\u83b7\u53d6\u5806\u6808\u9876\u90e8\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>int getMin()<\/code>&nbsp;\u83b7\u53d6\u5806\u6808\u4e2d\u7684\u6700\u5c0f\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong><br>[\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"]<br>[[],[-2],[0],[-3],[],[],[],[]]<br><br><strong>\u8f93\u51fa\uff1a<\/strong>[null,null,null,null,-3,null,0,-2]<\/pre>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong> <\/p>\n\n\n\n<p>MinStack minStack = new MinStack(); <\/p>\n\n\n\n<p>minStack.push(-2); <\/p>\n\n\n\n<p>minStack.push(0); <\/p>\n\n\n\n<p>minStack.push(-3); <\/p>\n\n\n\n<p>minStack.getMin(); --&gt; \u8fd4\u56de -3. <\/p>\n\n\n\n<p>minStack.pop(); <\/p>\n\n\n\n<p>minStack.top(); --&gt; \u8fd4\u56de 0. <\/p>\n\n\n\n<p>minStack.getMin(); --&gt; \u8fd4\u56de -2.<\/p>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u6b63\u5e38\u6808\u903b\u8f91\uff0c\u5e76\u989d\u5916\u5b58\u50a8\u524d\u7f00\u6700\u5c0f\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MinStack:\n\n    def __init__(self):\n        self.st=&#91;(0,inf)]\n\n    def push(self, val: int) -&gt; None:\n        self.st.append((val,min(self.st&#91;-1]&#91;1],val)))\n        \n\n    def pop(self) -&gt; None:\n        self.st.pop()\n\n    def top(self) -&gt; int:\n        return self.st&#91;-1]&#91;0]\n\n    def getMin(self) -&gt; int:\n        return self.st&#91;-1]&#91;1]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">59.<a href=\"https:\/\/leetcode.cn\/problems\/valid-parentheses\/\" target=\"_blank\"  rel=\"nofollow\" >\u6709\u6548\u7684\u62ec\u53f7<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u62ec&nbsp;<code>'('<\/code>\uff0c<code>')'<\/code>\uff0c<code>'{'<\/code>\uff0c<code>'}'<\/code>\uff0c<code>'['<\/code>\uff0c<code>']'<\/code>&nbsp;\u7684\u5b57\u7b26\u4e32&nbsp;<code>s<\/code>&nbsp;\uff0c\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u6709\u6548\u3002<\/p>\n\n\n\n<p>\u6709\u6548\u5b57\u7b26\u4e32\u9700\u6ee1\u8db3\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5de6\u62ec\u53f7\u5fc5\u987b\u7528\u76f8\u540c\u7c7b\u578b\u7684\u53f3\u62ec\u53f7\u95ed\u5408\u3002<\/li>\n\n\n\n<li>\u5de6\u62ec\u53f7\u5fc5\u987b\u4ee5\u6b63\u786e\u7684\u987a\u5e8f\u95ed\u5408\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u53f3\u62ec\u53f7\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u76f8\u540c\u7c7b\u578b\u7684\u5de6\u62ec\u53f7\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>s = \"()\"<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>true<\/p>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u68c0\u6d4b\u5230\u53f3\u62ec\u53f7\u65f6\u5224\u65adpop\u5143\u7d20\u662f\u5426\u4e3a\u5bf9\u5e94\u5de6\u62ec\u53f7\uff0c\u5bf9\u5219\u7ee7\u7eed\uff0c\u4e0d\u5bf9\u5219\u8fd4\u56defa'l'se<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def isValid(self, s: str) -&gt; bool:\n        if len(s)%2:\n            return False\n        mp={')':'(',']':'&#91;','}':'{'}\n        d=&#91;]\n        for i in s:\n            if i not in mp:\n                d.append(i)\n            elif not d or d.pop()!=mp&#91;i]:\n                return False\n        return not d<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">60.<a href=\"https:\/\/leetcode.cn\/problems\/best-time-to-buy-and-sell-stock\/\" target=\"_blank\"  rel=\"nofollow\" >\u4e70\u5356\u80a1\u7968\u7684\u6700\u4f73\u65f6\u673a<\/a><\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>prices<\/code>&nbsp;\uff0c\u5b83\u7684\u7b2c&nbsp;<code>i<\/code>&nbsp;\u4e2a\u5143\u7d20&nbsp;<code>prices[i]<\/code>&nbsp;\u8868\u793a\u4e00\u652f\u7ed9\u5b9a\u80a1\u7968\u7b2c&nbsp;<code>i<\/code>&nbsp;\u5929\u7684\u4ef7\u683c\u3002<\/p>\n\n\n\n<p>\u4f60\u53ea\u80fd\u9009\u62e9&nbsp;<strong>\u67d0\u4e00\u5929<\/strong>&nbsp;\u4e70\u5165\u8fd9\u53ea\u80a1\u7968\uff0c\u5e76\u9009\u62e9\u5728&nbsp;<strong>\u672a\u6765\u7684\u67d0\u4e00\u4e2a\u4e0d\u540c\u7684\u65e5\u5b50<\/strong>&nbsp;\u5356\u51fa\u8be5\u80a1\u7968\u3002\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u4f60\u53ef\u4ee5\u4ece\u8fd9\u7b14\u4ea4\u6613\u4e2d\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002\u5982\u679c\u4f60\u4e0d\u80fd\u83b7\u53d6\u4efb\u4f55\u5229\u6da6\uff0c\u8fd4\u56de&nbsp;<code>0<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>[7,1,5,3,6,4]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>5<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u5728\u7b2c 2 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 6\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u6700\u5927\u5229\u6da6 = 6-1 = 5 \u3002<\/li>\n\n\n\n<li>     \u6ce8\u610f\u5229\u6da6\u4e0d\u80fd\u662f 7-1 = 6, \u56e0\u4e3a\u5356\u51fa\u4ef7\u683c\u9700\u8981\u5927\u4e8e\u4e70\u5165\u4ef7\u683c\uff1b\u540c\u65f6\uff0c\u4f60\u4e0d\u80fd\u5728\u4e70\u5165\u524d\u5356\u51fa\u80a1\u7968\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u5b58\u4e0b\u5f53\u524d\u6700\u5927\u5229\u6da6\u4e0e\u524d\u7f00\u6700\u5c0f\u503c\uff0c\u904d\u5386\u65b0\u4ef7\u683c\u51cf\u53bb\u524d\u7f00\u6700\u5c0f\u503c\uff0c\u5f97\u5230\u65b0\u5229\u6da6\u5e76\u5bf9\u6bd4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def maxProfit(self, prices: List&#91;int]) -&gt; int:\n        ans=0\n        min_num=prices&#91;0]\n        for i in prices:\n            ans=max(ans,i-min_num)\n            min_num=min(min_num,i)\n        return ans<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">61.<a href=\"https:\/\/leetcode.cn\/problems\/jump-game\/\" target=\"_blank\"  rel=\"nofollow\" >\u8df3\u8dc3\u6e38\u620f<\/a><\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u4f60\u6700\u521d\u4f4d\u4e8e\u6570\u7ec4\u7684&nbsp;<strong>\u7b2c\u4e00\u4e2a\u4e0b\u6807<\/strong>&nbsp;\u3002\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4f60\u5728\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u8df3\u8dc3\u7684\u6700\u5927\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u5224\u65ad\u4f60\u662f\u5426\u80fd\u591f\u5230\u8fbe\u6700\u540e\u4e00\u4e2a\u4e0b\u6807\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u8fd4\u56de&nbsp;<code>true<\/code>&nbsp;\uff1b\u5426\u5219\uff0c\u8fd4\u56de&nbsp;<code>false<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [2,3,1,1,4]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>true<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u53ef\u4ee5\u5148\u8df3 1 \u6b65\uff0c\u4ece\u4e0b\u6807 0 \u5230\u8fbe\u4e0b\u6807 1, \u7136\u540e\u518d\u4ece\u4e0b\u6807 1 \u8df3 3 \u6b65\u5230\u8fbe\u6700\u540e\u4e00\u4e2a\u4e0b\u6807\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u9010\u6b65\u6269\u5f20\u53ef\u5230\u8fbe\u533a\u57df\uff0c\u82e5i\u8d85\u51fa\u533a\u57df\uff0c\u8fd4\u56deFalse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def canJump(self, nums: List&#91;int]) -&gt; bool:\n        mx = 0\n        for i, jump in enumerate(nums):\n            if i &gt; mx:  # \u65e0\u6cd5\u5230\u8fbe i\n                return False\n            mx = max(mx, i + jump)  # \u4ece i \u6700\u53f3\u53ef\u4ee5\u8df3\u5230 i+jump\n        return True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">62.<a href=\"https:\/\/leetcode.cn\/problems\/climbing-stairs\/\" target=\"_blank\"  rel=\"nofollow\" >\u722c\u697c\u68af<\/a><\/h2>\n\n\n\n<p>\u5047\u8bbe\u4f60\u6b63\u5728\u722c\u697c\u68af\u3002\u9700\u8981&nbsp;<code>n<\/code>&nbsp;\u9636\u4f60\u624d\u80fd\u5230\u8fbe\u697c\u9876\u3002<\/p>\n\n\n\n<p>\u6bcf\u6b21\u4f60\u53ef\u4ee5\u722c&nbsp;<code>1<\/code>&nbsp;\u6216&nbsp;<code>2<\/code>&nbsp;\u4e2a\u53f0\u9636\u3002\u4f60\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u722c\u5230\u697c\u9876\u5462\uff1f<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>n = 2<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>2<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002<\/li>\n\n\n\n<li>1. 1 \u9636 + 1 \u9636<\/li>\n\n\n\n<li>2. 2 \u9636<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u6709\u4e24\u79cd\u8d70\u6cd5\uff0c\u8d70\u4e00\u6b65\u6216\u8005\u8d70\u4e24\u6b65\uff0c\u6240\u4ee5dp[i]=dp[i-1]+dp[i-1]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def climbStairs(self, n: int) -&gt; int:\n        dp=&#91;0]*(n+1)\n        dp&#91;0]=1\n        dp&#91;1]=1\n        for i in range(2,n+1):\n            dp&#91;i]=dp&#91;i-1]+dp&#91;i-2]\n        return dp&#91;n]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">63.<a href=\"https:\/\/leetcode.cn\/problems\/sort-an-array\/\" target=\"_blank\"  rel=\"nofollow\" >\u6392\u5e8f\u6570\u7ec4<\/a>(\u624b\u6495\u5feb\u6392)<\/h2>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u8bf7\u4f60\u5c06\u8be5\u6570\u7ec4\u5347\u5e8f\u6392\u5217\u3002<\/p>\n\n\n\n<p>\u4f60\u5fc5\u987b\u5728&nbsp;<strong>\u4e0d\u4f7f\u7528\u4efb\u4f55\u5185\u7f6e\u51fd\u6570<\/strong>&nbsp;\u7684\u60c5\u51b5\u4e0b\u89e3\u51b3\u95ee\u9898\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(nlog(n))<\/code>\uff0c\u5e76\u4e14\u7a7a\u95f4\u590d\u6742\u5ea6\u5c3d\u53ef\u80fd\u5c0f\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\uff1a<\/strong>nums = [5,2,3,1]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,5]<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\uff1a<\/strong>\u6570\u7ec4\u6392\u5e8f\u540e\uff0c\u67d0\u4e9b\u6570\u5b57\u7684\u4f4d\u7f6e\u6ca1\u6709\u6539\u53d8\uff08\u4f8b\u5982\uff0c2 \u548c 3\uff09\uff0c\u800c\u5176\u4ed6\u6570\u5b57\u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u6539\u53d8\uff08\u4f8b\u5982\uff0c1 \u548c 5\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u5b9a\u4e49\u4e00\u4e2apartition\u5b50\u51fd\u6570\uff0c\u968f\u673a\u5728\u6b64\u533a\u95f4\u5185\u6311\u4e00\u4e2a\u6570pivot\u4e0eleft\u4f4d\u7f6e\u7684\u6570\u4ea4\u6362\uff0c\u518d\u4ece\u5de6\u53f3\u5411\u4e2d\u95f4\u904d\u5386\uff0c\u5c0f\u4e8epivot\u7684\u5de6\u7f6e\uff0c\u5927\u4e8epivot\u7684\u53f3\u7f6e\uff0c\u6700\u540er\u4f1a\u6307\u5411\u6700\u540e\u4e00\u4e2a\u5c0f\u4e8epivot\u7684\u6570\uff0c\u5c06pivot\u4e0e\u6b64\u6570\u4ea4\u6362\u4f4d\u7f6e\uff0c\u8fd4\u56depivot\u6700\u7ec8\u7684\u4f4d\u7f6e<\/p>\n\n\n\n<p>\u5b9a\u4e49\u5feb\u6392\u9012\u5f52\u51fd\u6570quicksort\uff0c\u5f53\u6570\u7ec4\u6709\u5e8f\u65f6\u76f4\u63a5\u8fd4\u56de\uff0c\u65e0\u5e8f\u65f6\u6267\u884cpartition\u51fd\u6570\uff0c\u8fd4\u56de\u5206\u754c\u7ebfi\uff0c\u56e0\u4e3a\u5de6\u8fb9\u5168\u662f\u5c0f\u4e8epivot\u7684\uff0c\u53f3\u8fb9\u5168\u662f\u5927\u4e8epivot\u7684\uff0c\u56e0\u6b64pivot\u7684\u4f4d\u7f6e\u662f\u56fa\u5b9a\u7684\u3002\u518d\u7528quicksort\u5206\u522b\u9012\u5f52(left,i-1)\u548c\uff08i+1,right\uff09\uff0c\u5c06\u672a\u786e\u5b9a\u90e8\u5206\u786e\u5b9a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def partition(self,nums: List&#91;int],Left:int,right:int )-&gt;int:\n        i=randint(Left,right)\n        pivot=nums&#91;i]\n        nums&#91;i],nums&#91;Left]=nums&#91;Left],nums&#91;i]\n        l,r=Left+1,right\n        while True:\n            while l&lt;=r and nums&#91;l]&lt;pivot:\n                l+=1\n            while l&lt;=r and nums&#91;r]&gt;pivot:\n                r-=1\n            if l&gt;=r:\n                break\n            nums&#91;l],nums&#91;r]=nums&#91;r],nums&#91;l]\n            l+=1\n            r-=1\n        nums&#91;Left],nums&#91;r] =nums&#91;r],nums&#91;Left]\n        return r \n    \n\n    def sortArray(self, nums: List&#91;int]) -&gt; List&#91;int]:\n        def quicksort(Left:int ,right:int)-&gt;None:\n            ordered=True\n            for i in range(Left,right):\n                if nums&#91;i]&gt;nums&#91;i+1]:\n                    ordered=False\n                    break\n            if ordered:\n                return\n            i=self.partition(nums,Left,right)\n            quicksort(Left,i-1)\n            quicksort(i+1,right)\n\n        quicksort(0,len(nums)-1)\n        return nums<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">64.<a href=\"https:\/\/leetcode.cn\/problems\/implement-trie-prefix-tree\/\" target=\"_blank\"  rel=\"nofollow\" >\u5b9e\u73b0 Trie (\u524d\u7f00\u6811)<\/a><\/h2>\n\n\n\n<p><strong><a href=\"https:\/\/baike.baidu.com\/item\/%E5%AD%97%E5%85%B8%E6%A0%91\/9825209?fr=aladdin\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >Trie<\/a><\/strong>\uff08\u53d1\u97f3\u7c7b\u4f3c \"try\"\uff09\u6216\u8005\u8bf4&nbsp;<strong>\u524d\u7f00\u6811<\/strong>&nbsp;\u662f\u4e00\u79cd\u6811\u5f62\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u68c0\u7d22\u5b57\u7b26\u4e32\u6570\u636e\u96c6\u4e2d\u7684\u952e\u3002\u8fd9\u4e00\u6570\u636e\u7ed3\u6784\u6709\u76f8\u5f53\u591a\u7684\u5e94\u7528\u60c5\u666f\uff0c\u4f8b\u5982\u81ea\u52a8\u8865\u5168\u548c\u62fc\u5199\u68c0\u67e5\u3002<\/p>\n\n\n\n<p>\u8bf7\u4f60\u5b9e\u73b0 Trie \u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Trie()<\/code>\u00a0\u521d\u59cb\u5316\u524d\u7f00\u6811\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>void insert(String word)<\/code>\u00a0\u5411\u524d\u7f00\u6811\u4e2d\u63d2\u5165\u5b57\u7b26\u4e32\u00a0<code>word<\/code>\u00a0\u3002<\/li>\n\n\n\n<li><code>boolean search(String word)<\/code>\u00a0\u5982\u679c\u5b57\u7b26\u4e32\u00a0<code>word<\/code>\u00a0\u5728\u524d\u7f00\u6811\u4e2d\uff0c\u8fd4\u56de\u00a0<code>true<\/code>\uff08\u5373\uff0c\u5728\u68c0\u7d22\u4e4b\u524d\u5df2\u7ecf\u63d2\u5165\uff09\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u00a0<code>false<\/code>\u00a0\u3002<\/li>\n\n\n\n<li><code>boolean startsWith(String prefix)<\/code>\u00a0\u5982\u679c\u4e4b\u524d\u5df2\u7ecf\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u00a0<code>word<\/code>\u00a0\u7684\u524d\u7f00\u4e4b\u4e00\u4e3a\u00a0<code>prefix<\/code>\u00a0\uff0c\u8fd4\u56de\u00a0<code>true<\/code>\u00a0\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u00a0<code>false<\/code>\u00a0\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u5165<\/strong><\/li>\n\n\n\n<li>[\"Trie\", \"insert\", \"search\", \"search\", \"startsWith\", \"insert\", \"search\"]<\/li>\n\n\n\n<li>[[], [\"apple\"], [\"apple\"], [\"app\"], [\"app\"], [\"app\"], [\"app\"]]<\/li>\n\n\n\n<li><strong>\u8f93\u51fa<\/strong>[null, null, true, false, true, null, true]<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u91ca<\/strong> <\/p>\n\n\n\n<p>Trie trie = new Trie(); <\/p>\n\n\n\n<p>trie.insert(\"apple\");<\/p>\n\n\n\n<p> trie.search(\"apple\"); \/\/ \u8fd4\u56de True<\/p>\n\n\n\n<p> trie.search(\"app\"); \/\/ \u8fd4\u56de <\/p>\n\n\n\n<p>False trie.startsWith(\"app\"); \/\/ \u8fd4\u56de True <\/p>\n\n\n\n<p>trie.insert(\"app\"); <\/p>\n\n\n\n<p>trie.search(\"app\"); \/\/ \u8fd4\u56de True<\/p>\n\n\n\n<p>\u89e3\uff1a<\/p>\n\n\n\n<p>\u5b9a\u4e49\u6811\u8282\u70b9\u5305\u542b son={}\u548c\u7ed3\u675f\u6807\u5fd7end<\/p>\n\n\n\n<p>\u6784\u9020\u6811\u65f6\u5728\u8282\u70b9\u7684son\u4e2d\u67e5\u627e\u662f\u5426\u6709\u6b64\u5143\u7d20c\uff0c\u6ca1\u6709\u5219\u52a0\u5165\u6b64\u5143\u7d20cur.son[c],\u5e76\u79fb\u52a8cur\u81f3cur.son[c]\uff0c\u5b58\u5728\u5219\u76f4\u63a5\u79fb\u52a8\u81f3cur.son[c]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Node:\n    def __init__(self):\n        self.son={}\n        self.end=False\n\nclass Trie:\n\n    def __init__(self):\n        self.root=Node()\n        \n\n    def insert(self, word: str) -> None:\n        cur=self.root\n        for c in word:\n            if c not in cur.son:\n                cur.son&#91;c]=Node()\n            cur=cur.son&#91;c]\n        cur.end=True\n\n        \n    def find(self, word: str)->int:\n        cur=self.root\n        for  c in word:\n            if c in cur.son:\n                cur=cur.son&#91;c]\n            else:\n                return 0\n        return 2 if cur.end==True else 1 \n\n\n\n    def search(self, word: str) -> bool:\n        if self.find(word)==2:\n            return True\n        else:\n            return False\n        \n\n    def startsWith(self, prefix: str) -> bool:\n        if self.find(prefix):\n            return True\n        else :\n            return False<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4e24\u6570\u4e4b\u548c \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa&#038;nbs &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LeetCode Hot 100\u5237\u9898\u7b14\u8bb0 - Sunsyblog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100\u5237\u9898\u7b14\u8bb0\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0 - Sunsyblog\" \/>\n<meta property=\"og:description\" content=\"1.\u4e24\u6570\u4e4b\u548c \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa&amp;nbs ...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100\u5237\u9898\u7b14\u8bb0\/\" \/>\n<meta property=\"og:site_name\" content=\"Sunsyblog\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-04T08:37:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T08:35:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg\" \/>\n<meta name=\"author\" content=\"Sunsy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sunsy\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/\",\"url\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/\",\"name\":\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0 - Sunsyblog\",\"isPartOf\":{\"@id\":\"https:\/\/sunsyblog.cn\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg\",\"datePublished\":\"2026-03-04T08:37:59+00:00\",\"dateModified\":\"2026-03-16T08:35:49+00:00\",\"author\":{\"@id\":\"https:\/\/sunsyblog.cn\/#\/schema\/person\/3cf3cdc8731e915f0d9f81cb9ca82712\"},\"breadcrumb\":{\"@id\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#primaryimage\",\"url\":\"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg\",\"contentUrl\":\"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/sunsyblog.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/sunsyblog.cn\/#website\",\"url\":\"https:\/\/sunsyblog.cn\/\",\"name\":\"Sunsyblog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/sunsyblog.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/sunsyblog.cn\/#\/schema\/person\/3cf3cdc8731e915f0d9f81cb9ca82712\",\"name\":\"Sunsy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/sunsyblog.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/weavatar.com\/avatar\/aa5b790f3d479fd400751c087e871f92282767b1cbde478b4961ba50ad307f05?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/weavatar.com\/avatar\/aa5b790f3d479fd400751c087e871f92282767b1cbde478b4961ba50ad307f05?s=96&d=mm&r=g\",\"caption\":\"Sunsy\"},\"sameAs\":[\"http:\/\/124.220.189.177\"],\"url\":\"https:\/\/sunsyblog.cn\/index.php\/author\/sunsy\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0 - Sunsyblog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100\u5237\u9898\u7b14\u8bb0\/","og_locale":"zh_CN","og_type":"article","og_title":"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0 - Sunsyblog","og_description":"1.\u4e24\u6570\u4e4b\u548c \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa&nbs ...","og_url":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100\u5237\u9898\u7b14\u8bb0\/","og_site_name":"Sunsyblog","article_published_time":"2026-03-04T08:37:59+00:00","article_modified_time":"2026-03-16T08:35:49+00:00","og_image":[{"url":"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg","type":"","width":"","height":""}],"author":"Sunsy","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"Sunsy","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"18 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/","url":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/","name":"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0 - Sunsyblog","isPartOf":{"@id":"https:\/\/sunsyblog.cn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#primaryimage"},"image":{"@id":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#primaryimage"},"thumbnailUrl":"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg","datePublished":"2026-03-04T08:37:59+00:00","dateModified":"2026-03-16T08:35:49+00:00","author":{"@id":"https:\/\/sunsyblog.cn\/#\/schema\/person\/3cf3cdc8731e915f0d9f81cb9ca82712"},"breadcrumb":{"@id":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#primaryimage","url":"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg","contentUrl":"https:\/\/aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com\/aliyun-lc-upload\/uploads\/2018\/07\/25\/question_11.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/sunsyblog.cn\/index.php\/2026\/03\/04\/leetcode-hot-100%e5%88%b7%e9%a2%98%e7%ac%94%e8%ae%b0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/sunsyblog.cn\/"},{"@type":"ListItem","position":2,"name":"LeetCode Hot 100\u5237\u9898\u7b14\u8bb0"}]},{"@type":"WebSite","@id":"https:\/\/sunsyblog.cn\/#website","url":"https:\/\/sunsyblog.cn\/","name":"Sunsyblog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sunsyblog.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/sunsyblog.cn\/#\/schema\/person\/3cf3cdc8731e915f0d9f81cb9ca82712","name":"Sunsy","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/sunsyblog.cn\/#\/schema\/person\/image\/","url":"https:\/\/weavatar.com\/avatar\/aa5b790f3d479fd400751c087e871f92282767b1cbde478b4961ba50ad307f05?s=96&d=mm&r=g","contentUrl":"https:\/\/weavatar.com\/avatar\/aa5b790f3d479fd400751c087e871f92282767b1cbde478b4961ba50ad307f05?s=96&d=mm&r=g","caption":"Sunsy"},"sameAs":["http:\/\/124.220.189.177"],"url":"https:\/\/sunsyblog.cn\/index.php\/author\/sunsy\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Sunsy","author_link":"https:\/\/sunsyblog.cn\/index.php\/author\/sunsy\/"},"uagb_comment_info":0,"uagb_excerpt":"1.\u4e24\u6570\u4e4b\u548c \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums&nbsp;\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c&nbsp;target\uff0c\u8bf7\u4f60\u5728&hellip;","_links":{"self":[{"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":41,"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/55\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunsyblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}