{"id":851,"date":"2025-07-14T20:19:00","date_gmt":"2025-07-14T12:19:00","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=851"},"modified":"2025-08-04T12:23:21","modified_gmt":"2025-08-04T04:23:21","slug":"hot100-%e5%8f%8c%e6%8c%87%e9%92%88","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=851","title":{"rendered":"Hot100 \u53cc\u6307\u9488"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cc\u6307\u9488\u5e38\u89c1\u7684<strong>\u4e09\u5927\u7c7b<\/strong>\u7528\u6cd5\uff1a<\/h2>\n\n\n\n<p>\u5176\u5b9e\u53cc\u6307\u9488\u53ef\u4ee5\u5f52\u4e3a <strong>\u4e09\u7c7b\u7b56\u7565<\/strong>\uff0c\u5206\u522b\u662f\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5bf9\u649e\u6307\u9488\uff08Two Pointers from Both Ends\uff09<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e24\u4e2a\u6307\u9488\u4ece\u6570\u7ec4\/\u5b57\u7b26\u4e32\u4e24\u7aef\u5411\u4e2d\u95f4\u9760\u62e2<\/strong><\/li>\n\n\n\n<li><strong>\u5e94\u7528\u573a\u666f\uff1a<\/strong> \u6709\u5e8f\u6570\u7ec4\u6c42\u548c\u3001\u56de\u6587\u5224\u65ad\u3001\u5de6\u53f3\u903c\u8fd1\u89e3\u7a7a\u95f4<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u6ed1\u52a8\u7a97\u53e3\uff08Sliding Window\uff09<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e24\u4e2a\u6307\u9488\u4ece\u540c\u4e00\u7aef\u51fa\u53d1\uff0c\u7ef4\u62a4\u4e00\u4e2a\u300c\u7a97\u53e3\u300d<\/strong><\/li>\n\n\n\n<li>\u4e00\u822c\u662f<strong>\u53f3\u6307\u9488\u62d3\u5c55\u7a97\u53e3\uff0c\u5de6\u6307\u9488\u6536\u7f29\u7a97\u53e3<\/strong><\/li>\n\n\n\n<li><strong>\u5e94\u7528\u573a\u666f\uff1a<\/strong> \u5b50\u4e32\u3001\u5b50\u6570\u7ec4\u95ee\u9898\uff0c\u6700\u5c0f\u957f\u5ea6\/\u6700\u5927\u957f\u5ea6<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u5feb\u6162\u6307\u9488\uff08Fast and Slow Pointer\uff09<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e24\u4e2a\u6307\u9488\u901f\u5ea6\u4e0d\u540c\uff08\u5e38\u7528\u4e8e\u94fe\u8868\uff09<\/strong><\/li>\n\n\n\n<li>\u5feb\u6307\u9488\u6bcf\u6b21\u79fb\u52a8\u4e24\u6b65\uff0c\u6162\u6307\u9488\u4e00\u6b65<\/li>\n\n\n\n<li><strong>\u5e94\u7528\u573a\u666f\uff1a<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5224\u65ad\u94fe\u8868\u6709\u65e0\u73af<\/li>\n\n\n\n<li>\u627e\u73af\u7684\u5165\u53e3<\/li>\n\n\n\n<li>\u627e\u4e2d\u70b9\u3001\u5220\u9664\u5012\u6570\u7b2c k \u4e2a\u8282\u70b9<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">283.\u79fb\u52a8\u96f6<\/h1>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 <code>nums<\/code>\uff0c\u5c06\u6240\u6709 <code>0<\/code> \u79fb\u52a8\u5230\u6570\u7ec4\u672b\u5c3e\uff0c\u540c\u65f6\u4fdd\u6301\u975e\u96f6\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d8\u3002<strong>\u539f\u5730\u64cd\u4f5c<\/strong>\uff0c\u4e0d\u4f7f\u7528\u989d\u5916\u7a7a\u95f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public void moveZeroes(int&#91;] nums) {\n        int zeroP = 0;\n        int p = 0;\n\n        for (; p &lt; nums.length; p++) {\n            if (nums&#91;p]!=0){\n                int temp = nums&#91;p];\n                nums&#91;p] = nums&#91;zeroP];\n                nums&#91;zeroP] = temp;\n                zeroP++;\n            }\n        }\n    }\n    \n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u4e24\u4e2a\u6307\u9488\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>p<\/code>\uff1a<strong>\u5feb\u6307\u9488<\/strong>\uff0c\u7528\u4e8e\u904d\u5386\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><code>zeroP<\/code>\uff1a<strong>\u6162\u6307\u9488<\/strong>\uff0c\u7528\u4e8e\u6307\u5411\u4e0b\u4e00\u4e2a\u8981\u653e\u7f6e\u975e\u96f6\u5143\u7d20\u7684\u4f4d\u7f6e\uff08\u4e5f\u5c31\u662f\u201c\u9047\u5230\u7684\u7b2c\u4e00\u4e2a 0 \u7684\u4f4d\u7f6e\u201d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u904d\u5386\u903b\u8f91\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5feb\u6307\u9488 <code>p<\/code> \u6bcf\u4e00\u6b65\u90fd\u5728\u5411\u524d\u8d70\u3002<\/li>\n\n\n\n<li>\u5f53 <code>nums[p] != 0<\/code>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5c06\u5f53\u524d <code>nums[p]<\/code> \u4e0e <code>nums[zeroP]<\/code> \u4ea4\u6362\u3002<\/li>\n\n\n\n<li>\u7136\u540e <code>zeroP++<\/code>\uff0c\u610f\u5473\u7740\u4e0b\u4e00\u4e2a\u975e\u96f6\u5143\u7d20\u5e94\u8be5\u653e\u5728\u4e0b\u4e00\u4e2a 0 \u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">11.\u76db\u6700\u591a\u6c34\u7684\u5bb9\u5668<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/container-with-most-water\/\">11. \u76db\u6700\u591a\u6c34\u7684\u5bb9\u5668<\/a><\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>height<\/code>\uff0c\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u5750\u6807\u8f74\u4e0a\u4e00\u6761\u7ad6\u7ebf\u7684\u9ad8\u5ea6\u3002\u6211\u4eec\u9009\u62e9\u5176\u4e2d\u4efb\u610f\u4e24\u6761\u7ebf\uff0c\u4e0e x \u8f74\u6784\u6210\u4e00\u4e2a\u5bb9\u5668\uff0c\u5176\u5bb9\u91cf\u7531<strong>\u4e24\u7ebf\u4e4b\u95f4\u7684\u8ddd\u79bb\u4e0e\u8f83\u77ed\u7684\u7ebf\u51b3\u5b9a<\/strong>\u3002\u4f60\u7684\u4efb\u52a1\u662f\u627e\u51fa\u8fd9\u4e2a\u5bb9\u5668\u80fd\u5bb9\u7eb3\u7684\u6700\u591a\u6c34\u91cf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf \u89e3\u9898\u5173\u952e\u70b9<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e24\u7ebf\u4e4b\u95f4\u8ddd\u79bb\u8d8a\u8fdc\u8d8a\u597d<\/strong><\/li>\n\n\n\n<li><strong>\u9ad8\u5ea6\u4ee5\u8f83\u77ed\u7684\u90a3\u4e00\u6761\u4e3a\u51c6<\/strong><\/li>\n\n\n\n<li><strong>\u76ee\u6807\u662f\u4f7f\u9762\u79ef = (\u8ddd\u79bb) \u00d7 (\u8f83\u5c0f\u9ad8\u5ea6) \u6700\u5927<\/strong><\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u4e2a\u9898\u76ee\u7684\u5173\u952e\u662f\uff1a<strong>\u5bfb\u627e\u6700\u4f18\u7ec4\u5408\uff0c\u904d\u5386\u6240\u6709\u7ec4\u5408\u592a\u6162\uff08O(n\u00b2)\uff09<\/strong>\uff0c\u6240\u4ee5\u6211\u4eec\u5f15\u5165\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u7b56\u7565 \u2014\u2014 <strong>\u53cc\u6307\u9488\u6cd5<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 \u53cc\u6307\u9488\u601d\u60f3\u89e3\u6790<\/h2>\n\n\n\n<p>\u4f7f\u7528\u4e24\u4e2a\u6307\u9488 <code>left<\/code> \u548c <code>right<\/code> \u5206\u522b\u6307\u5411\u6570\u7ec4\u4e24\u7aef\uff0c\u8868\u793a\u5f53\u524d\u8003\u8651\u7684\u4e24\u6761\u8fb9\u3002\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u5f62\u6210\u7684\u662f\u6700\u5bbd\u7684\u5bb9\u5668\u3002<\/p>\n\n\n\n<p>\u6838\u5fc3\u7b56\u7565\u4e3a\uff1a<strong>\u6bcf\u6b21\u79fb\u52a8\u9ad8\u5ea6\u8f83\u5c0f\u7684\u4e00\u7aef<\/strong>\u3002<br>\u5176\u539f\u56e0\u5728\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bb9\u5668\u5bb9\u91cf\u53d7\u9650\u4e8e\u4e24\u8fb9\u4e2d\u7684\u8f83\u5c0f\u9ad8\u5ea6\uff1b<\/li>\n\n\n\n<li>\u4fdd\u7559\u8f83\u77ed\u8fb9\u3001\u79fb\u52a8\u8f83\u957f\u8fb9\u65e0\u6cd5\u63d0\u5347\u9ad8\u5ea6\uff0c\u4e14\u5bbd\u5ea6\u51cf\u5c0f\uff0c\u5bb9\u91cf\u53ea\u4f1a\u51cf\u5c11\uff1b<\/li>\n\n\n\n<li>\u79fb\u52a8\u8f83\u77ed\u8fb9\uff0c\u53ef\u80fd\u83b7\u5f97\u66f4\u5927\u7684\u9ad8\u5ea6\uff0c\u4ece\u800c\u5728\u5bbd\u5ea6\u51cf\u5c0f\u65f6\u63d0\u5347\u5bb9\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int maxArea(int&#91;] height) {<br>    int left = 0;<br>    int right = height.length-1;<br><br>    int result = 0;<br>    while (!(left==right)){<br>        int tHeight=Math.<em>min<\/em>(height&#91;left],height&#91;right]);<br>        int size = tHeight*(right-left);<br>        if (size&gt;result){<br>            result=size;<br>        }<br>        if (height&#91;left]==tHeight){<br>            left++;<br>        }else{<br>            right--;<br>        }<br>    }<br><br>    return result;<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">15.\u4e09\u6570\u4e4b\u548c<\/h1>\n\n\n\n<p>\u4e24\u79cd\u601d\u8def\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u53cc\u6307\u9488\uff08\u4e09\u6307\u9488\uff09\uff1a<\/h2>\n\n\n\n<p>\u56fa\u5b9a\u4e00\u4e2afirst\uff0c\u53e6\u5916\u4e24\u4e2a\u540c\u5411\u79fb\u52a8\uff0c\u9047\u5230\u91cd\u590d\u7684\u8fde\u7eed\u8df3\u8fc7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public static List&lt;List&lt;Integer&gt;&gt; threeSum(int[] nums) {<br>    List&lt;List&lt;Integer&gt;&gt; result = new ArrayList&lt;&gt;();<br><br>    \/\/\u5148\u6392\u5e8f\uff0c\u4f7f\u5f97\u53cc\u5411\u6307\u9488\u79fb\u52a8\u6709\u903b\u8f91\u3001\u5e76\u4e14\u53ef\u4ee5\u53bb\u91cd<br>    Arrays.<em>sort<\/em>(nums);<br><br>    \/\/\u56fa\u5b9a\u4e00\u4e2afirst\uff0c\u53e6\u5916\u4e24\u4e2a\u540c\u5411\u79fb\u52a8\uff0c\u9047\u5230\u91cd\u590d\u7684\u8fde\u7eed\u8df3\u8fc7\u3002\u5f53first&gt;0\u5c31\u627e\u4e0d\u5230\u4e86\u3002<br>    for (int first = 0; first &lt; nums.length - 2; first++) {<br>        if (first &gt; 0 &amp;&amp; nums[first] == nums[first - 1]) continue;<br>        if (nums[first] &gt; 0) break;<br><br>        int left = first + 1;<br>        int right = nums.length - 1;<br><br>        while (left &lt; right) {<br>            int sum = nums[first] + nums[left] + nums[right];<br>            if (sum == 0) {<br>                result.add(Arrays.<em>asList<\/em>(nums[first], nums[left], nums[right]));<br>                left++;<br>                right--;<br>                \/\/\u53bb\u91cd<br>                while (left &lt; right &amp;&amp; nums[left] == nums[left - 1]) left++;<br>                while (left &lt; right &amp;&amp; nums[right] == nums[right + 1]) right--;<br>            } else if (sum &lt; 0) {<br>                \/\/sum&lt;0\u8bf4\u660e\u8981\u5f80\u5927\u8d70\uff0c\u5373left\u53f3\u79fb<br>                left++;<br>                \/\/\u53bb\u91cd<br>                while (left &lt; right &amp;&amp; nums[left] == nums[left - 1]) left++;<br>            } else {<br>                \/\/\u5bf9\u79f0\u903b\u8f91<br>                right--;<br>                \/\/\u53bb\u91cd<br>                while (left &lt; right &amp;&amp; nums[right] == nums[right + 1]) right--;<br>            }<br>        }<br>    }<br>    return result;<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u56fa\u5b9a\u4e00\u4e2a\uff0c\u201c\u4e24\u6570\u4e4b\u548c\u201d<\/h2>\n\n\n\n<p>\u7528hash\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static List&lt;List&lt;Integer&gt;&gt; threeSum(int&#91;] nums) {\n    List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;();\n    Arrays.sort(nums); \/\/ \u6392\u5e8f\u662f\u4e3a\u4e86\u540e\u7eed\u53bb\u91cd\u65b9\u4fbf\n\n    for (int i = 0; i &lt; nums.length - 2; i++) {\n        \/\/ \u8df3\u8fc7\u91cd\u590d a\n        if (i &gt; 0 &amp;&amp; nums&#91;i] == nums&#91;i - 1]) continue;\n\n        int target = -nums&#91;i];\n        Set&lt;Integer&gt; seen = new HashSet&lt;&gt;();\n        for (int j = i + 1; j &lt; nums.length; j++) {\n            int complement = target - nums&#91;j];\n            if (seen.contains(complement)) {\n                res.add(Arrays.asList(nums&#91;i], complement, nums&#91;j]));\n\n                \/\/ \u8df3\u8fc7\u91cd\u590d b\n                while (j + 1 &lt; nums.length &amp;&amp; nums&#91;j] == nums&#91;j + 1]) j++;\n            }\n            seen.add(nums&#91;j]);\n        }\n    }\n\n    return res;\n}\n<\/code><\/pre>\n\n\n\n<p>\u53e6\u5916\uff0c\u300c\u56db\u6570\u4e4b\u548c\u300d\u300ck\u6570\u4e4b\u548c\u300d\u7b49\u95ee\u9898\u7684<strong>\u901a\u7528\u89e3\u6cd5<\/strong>\u5c31\u662f\u901a\u8fc7<strong>\u9012\u5f52\u5730\u5c06\u95ee\u9898\u8f6c\u5316\u4e3a\u201c\u4e24\u6570\u4e4b\u548c\u201d<\/strong>\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">42.\u63a5\u96e8\u6c34<\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">public static int trap(int[] height) {<br>    \/\/\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O (n)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O (1)\u3002<br>    int ans=0;<br>    int left = 0;<br>    int right = height.length-1;<br>    int preMax = 0;<br>    int sufMax = 0;<br><br>    while (left&lt;=right){<br>        \/\/\u66f4\u65b0\u524d\u7f00\u6700\u5927\u503c\u548c\u540e\u7f00\u6700\u5927\u503c<br>        preMax = Math.<em>max<\/em>(height[left],preMax);<br>        sufMax = Math.<em>max<\/em>(height[right],sufMax);<br><br>        \/\/\u5f53\u524d\u4f4d\u7f6e\u63a5\u6c34\u9ad8\u5ea6\u53d6\u51b3\u4e8e\u5de6\u53f3\u8fb9\u754c\u4e2d\u8f83\u5c0f\u7684\u503c<br>        if (preMax&lt;sufMax){<br>            \/\/\u5982\u679c\u5f53\u524d\u524d\u7f00\u6700\u5927\u503c\u5c0f\u4e8e\u540e\u7f00\u6700\u5927\u503c\uff0c\u90a3\u4e48\u524d\u7f00\u6700\u5927\u503c\u5c31\u662f\u8981\u6c42\u7684\u201c\u8f83\u5c0f\u7684\u503c\u201d\uff0c\u56e0\u4e3a\u540e\u7f00\u6700\u5927\u503c\u4e0d\u80fd\u66f4\u5c0f\u4e86\u3002<br>            ans+=preMax-height[left];<br>            left++;<br>        }else {<br>            \/\/\u540c\u7406\u5bf9\u79f0\uff0c\u6b64\u5904\u5305\u542b\u76f8\u7b49\u7684\u903b\u8f91\uff0c\u76f8\u7b49\u65f6\u8c01\u51cf\u5e95\u5ea7\u90fd\u4e00\u6837\u3002<br>            ans+=sufMax-height[right];<br>            right--;<br>        }<br>    }<br><br>    return ans;<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53cc\u6307\u9488\u5e38\u89c1\u7684\u4e09\u5927\u7c7b\u7528\u6cd5\uff1a \u5176\u5b9e\u53cc\u6307\u9488\u53ef\u4ee5\u5f52\u4e3a \u4e09\u7c7b\u7b56\u7565\uff0c\u5206\u522b\u662f\uff1a 1. \u5bf9\u649e\u6307\u9488\uff08Two Pointers f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[75],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-3","tag-75"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=851"}],"version-history":[{"count":8,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":1087,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions\/1087"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}