{"id":813,"date":"2025-03-21T15:20:46","date_gmt":"2025-03-21T07:20:46","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=813"},"modified":"2025-08-05T22:32:33","modified_gmt":"2025-08-05T14:32:33","slug":"hot100-%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=813","title":{"rendered":"Hot100 \u6280\u5de7"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">136.\u53ea\u51fa\u73b0\u4e00\u6b21\u7684\u6570\u5b57<\/h1>\n\n\n\n<p>\u4f4d\u8fd0\u7b97\uff0c\u5f02\u6216<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f02\u6216\u7279\u70b9\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4ea4\u6362\u5f8b<\/strong><\/li>\n\n\n\n<li><strong>\u7ed3\u5408\u5f8b<\/strong><\/li>\n\n\n\n<li><strong>\u81ea\u8eab\u5f02\u6216\u4e3a 0<\/strong><\/li>\n\n\n\n<li><strong>\u4e0e 0 \u5f02\u6216\u4e0d\u53d8<\/strong><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/single-number\/description\/?envType=study-plan-v2&amp;envId=top-100-liked\">136. \u53ea\u51fa\u73b0\u4e00\u6b21\u7684\u6570\u5b57 &#8211; \u529b\u6263\uff08LeetCode\uff09<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/136. \u53ea\u51fa\u73b0\u4e00\u6b21\u7684\u6570\u5b57<br>public class SingleNumber_136 {<br>    static class Solution {<br>        \/\/\u5f02\u6216\u8fd0\u7b97\u6709\u52a0\u6cd5\u5f8b\u548c\u7ed3\u5408\u5f8b;\u81ea\u8eab\u5f02\u6216\u4e3a0\uff1b\u4e0e0\u5f02\u6216\u4e0d\u53d8<br>        public int singleNumber(int&#91;] nums) {<br>            int result = nums&#91;0];<br>            for (int i = 1; i &lt; nums.length; i++) {<br>                result=result^nums&#91;i];<br>            }<br>            return result;<br>        }<br>    }<br><br>    public static void main(String&#91;] args) {<br>        Solution solution = new Solution();<br>        int&#91;] nums = {4, 1, 2, 1, 2};<br>        int single = solution.singleNumber(nums);<br>        System.<em>out<\/em>.println(\"\u53ea\u51fa\u73b0\u4e00\u6b21\u7684\u6570\u5b57\u662f: \" + single);<br>    }<br><br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">169.\u591a\u6570\u5143\u7d20<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/majority-element\/\">169. \u591a\u6570\u5143\u7d20<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int majorityElement(int&#91;] nums) {\n        \/\/\u6709\u5df2\u77e5\u6761\u4ef6\uff1a\u6570\u7ec4\u975e\u7a7a\u3001\u5b58\u5728\u591a\u6570\u5143\u7d20\u3002\u6709\u56e0\u4e3a\u51fa\u73b0\u6b21\u6570 \u5927\u4e8e \u230a n\/2 \u230b \uff0c\u6240\u4ee5\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u591a\u6570\u5143\u7d20\u3002\n        \/\/\u904d\u5386\u6240\u6709\u5143\u7d20\uff0c\u4e0d\u76f8\u540c\u7684\u62b5\u6d88\uff0c\u5269\u4e0b\u7684\u5c31\u662f\u53ea\u6709\u6240\u6c42\u7684\u201c\u591a\u6570\u5143\u7d20\u201d\u3002\n        \/\/\u62b5\u6d88\u64cd\u4f5c\uff1a\u5bf9\u4e8e\u5f53\u524d\u5143\u7d20\u8ba1\u6570count\uff0c\u9047\u5230\u76f8\u540c\u5143\u7d20\uff0c\u8ba1\u6570+1\uff0c\u9047\u5230\u4e0d\u540c\u7684\u5143\u7d20\uff0c\u8ba1\u6570-1\uff1b\u5f53\u8ba1\u6570\u4e3a0\u65f6\uff0c\u5c06\u5f53\u524d\u5143\u7d20\u4f5c\u4e3a\u65b0\u7684\u5019\u9009\u5143\u7d20\u3002\n\n        int count = 0;\n        int current = 0;\n\n        for (int i = 0; i &lt; nums.length; i++) {\n            if (count==0){\n                current=nums&#91;i];\n            }\n            if (nums&#91;i]==current){\n                count++;\n            }else {\n                count--;\n            }\n        }\n\n        return current;\n    }<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">75.\u989c\u8272\u5206\u7c7b\uff1a<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/sort-colors\/\">75. \u989c\u8272\u5206\u7c7b<\/a><\/p>\n\n\n\n<p>\u6838\u5fc3\u601d\u60f3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u4e09\u4e2a\u6307\u9488\u5212\u5206\u533a\u57df\uff1a0 \u7684\u533a\u57df\u30011 \u7684\u533a\u57df\u548c 2 \u7684\u533a\u57df<\/li>\n\n\n\n<li><code>zero<\/code>\u6307\u9488\uff1a\u6307\u5411 0 \u533a\u57df\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff08\u5373\u4e0b\u4e00\u4e2a 0 \u5e94\u8be5\u653e\u7f6e\u7684\u4f4d\u7f6e\uff09<\/li>\n\n\n\n<li><code>two<\/code>\u6307\u9488\uff1a\u6307\u5411 2 \u533a\u57df\u7684\u524d\u4e00\u4e2a\u4f4d\u7f6e\uff08\u5373\u4e0b\u4e00\u4e2a 2 \u5e94\u8be5\u653e\u7f6e\u7684\u4f4d\u7f6e\uff09<\/li>\n\n\n\n<li><code>i<\/code>\u6307\u9488\uff1a\u7528\u4e8e\u904d\u5386\u6570\u7ec4<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521d\u59cb\u5316<code>zero<\/code>\u4e3a 0\uff0c<code>two<\/code>\u4e3a\u6570\u7ec4\u672b\u5c3e\u7d22\u5f15<\/li>\n\n\n\n<li>\u904d\u5386\u6570\u7ec4\uff0c\u5f53<code>i<\/code>\u8d85\u8fc7<code>two<\/code>\u65f6\u7ed3\u675f<\/li>\n\n\n\n<li>\u82e5\u5f53\u524d\u5143\u7d20\u662f 2\uff0c\u5219\u4e0e<code>two<\/code>\u6307\u9488\u4f4d\u7f6e\u5143\u7d20\u4ea4\u6362\uff0c<code>two<\/code>\u5de6\u79fb\uff08\u56e0\u4e3a\u4ea4\u6362\u8fc7\u6765\u7684\u5143\u7d20\u53ef\u80fd\u8fd8\u662f 2\uff0c\u9700\u8981\u7ee7\u7eed\u68c0\u67e5<\/li>\n\n\n\n<li>\u82e5\u5f53\u524d\u5143\u7d20\u662f 0\uff0c\u5219\u4e0e<code>zero<\/code>\u6307\u9488\u4f4d\u7f6e\u5143\u7d20\u4ea4\u6362\uff0c<code>zero<\/code>\u53f3\u79fb\uff0c<code>i<\/code>\u53f3\u79fb<\/li>\n\n\n\n<li>\u82e5\u5f53\u524d\u5143\u7d20\u662f 1\uff0c\u76f4\u63a5<code>i<\/code>\u53f3\u79fb<\/li>\n<\/ol>\n\n\n\n<p>\u56e0\u4e3ai\u4ece\u524d\u5f80\u540e\uff0czero\u4e5f\u4ece\u524d\u5f80\u540e\uff0c\u6240\u4ee5zero\u6362\u8fc7\u53bb\u548c\u6362\u8fc7\u6765\u7684\u503c\u77e5\u9053\u3002\u800c<code>two<\/code>\u662f\u4ece\u540e\u5f80\u524d\uff0c\u6362\u8fc7\u6765\u7684\u53ef\u80fd\u8fd8\u662f0\u30012 \u3002\u6240\u4ee5\u9700\u8981while\u5faa\u73af\u68c0\u67e5\u5904\u7406\uff0c\u800c\u5bf9\u96f6\u7684\u5224\u65ad\u5904\u7406\u4e5f\u56e0\u6b64\u8981\u653e\u5728\u5bf92\u5904\u7406\u7684\u540e\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void sortColors(int&#91;] nums) {<br>    int zero = 0;<br>    int two =nums.length-1;<br>    for (int i = 0; i &lt;= two; i++) {<br>        while (nums&#91;i]==2 &amp;&amp; i&lt;=two){<br>            int temp = nums&#91;i];<br>            nums&#91;i] = nums&#91;two];<br>            nums&#91;two] = temp;<br>            two--;<br>        }<br><br>        if (nums&#91;i]==0){<br>            int temp = nums&#91;i];<br>            nums&#91;i] = nums&#91;zero];<br>            nums&#91;zero] = temp;<br>            zero++;<br>        }<br>    }<br>}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">31.\u4e0b\u4e00\u4e2a\u6392\u5e8f\uff1a<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u6574\u4f53\u601d\u8def<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4ece\u540e\u5411\u524d\u627e\u7b2c\u4e00\u4e2a\u201c\u5347\u5e8f\u5bf9\u201d<\/strong><br>\u627e\u5230\u6700\u5927\u7d22\u5f15 <code>i<\/code>\uff0c\u4f7f\u5f97 <code>nums[i] &lt; nums[i + 1]<\/code><br>   \u8bf4\u660e\u4ece <code>i+1<\/code> \u5f00\u59cb\u662f\u4e00\u4e2a <strong>\u9012\u51cf\u5e8f\u5217<\/strong>\uff08\u5373\u5f53\u524d\u540e\u7f00\u6700\u5927\uff09<\/li>\n\n\n\n<li><strong>\u5982\u679c\u627e\u5230\u4e86\u8fd9\u6837\u7684 <code>i<\/code><\/strong>\uff08\u5373\u5f53\u524d\u4e0d\u662f\u6700\u5927\u6392\u5217\uff09\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4ece\u540e\u5411\u524d\u627e\u6700\u5927\u7d22\u5f15 <code>j<\/code>\uff0c\u4f7f\u5f97 <code>nums[j] &gt; nums[i]<\/code><\/li>\n\n\n\n<li>\u4ea4\u6362 <code>nums[i]<\/code> \u4e0e <code>nums[j]<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u53cd\u8f6c\u540e\u7f00<\/strong>\uff08\u4ece <code>i+1<\/code> \u5230\u672b\u5c3e\uff09<br>   \u628a\u539f\u672c\u9012\u51cf\u7684\u540e\u7f00\u53d8\u4e3a\u6700\u5c0f\u7684\u5347\u5e8f\u6392\u5217\uff0c\u786e\u4fdd\u201c\u521a\u521a\u5927\u4e00\u70b9\u201d<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7279\u6b8a\u60c5\u51b5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u6b65\u9aa4 1 \u627e\u4e0d\u5230\u4efb\u4f55 <code>i<\/code>\uff08\u5373\u5168\u662f\u9012\u51cf\u7684\u6392\u5217\uff0c\u5982 <code>[5,4,3,2,1]<\/code>\uff09\uff0c\u8bf4\u660e\u5df2\u7ecf\u662f\u6700\u5927\u6392\u5217<br>\u76f4\u63a5 <strong>\u53cd\u8f6c\u6574\u4e2a\u6570\u7ec4<\/strong>\uff0c\u53d8\u6210\u6700\u5c0f\u6392\u5217\uff08\u5347\u5e8f\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6b65\u9aa4<\/th><th>i<\/th><th>j<\/th><th>\u6570\u7ec4\u72b6\u6001<\/th><\/tr><\/thead><tbody><tr><td>\u539f\u59cb\u8f93\u5165<\/td><td><\/td><td><\/td><td><code>[1, 4, 5, 3, 2]<\/code><\/td><\/tr><tr><td>\u627e\u5230 i<\/td><td>1<\/td><td><\/td><td><code>nums[1]=4<\/code><\/td><\/tr><tr><td>\u627e\u5230 j<\/td><td><\/td><td>2<\/td><td><code>nums[2]=5<\/code><\/td><\/tr><tr><td>\u4ea4\u6362 i \u548c j<\/td><td><\/td><td><\/td><td><code>[1, 5, 4, 3, 2]<\/code><\/td><\/tr><tr><td>\u7ffb\u8f6c\u540e\u7f00<\/td><td><\/td><td><\/td><td><code>[1, 5, 2, 3, 4]<\/code> \u2705<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void nextPermutation(int&#91;] nums) {\n    \/\/1.\u627e\u5230\u7b2c\u4e00\u4e2a\u5347\u5e8f\u5bf9\uff1ai\uff0ci+1\n    int i = nums.length-2;\n    while (i>=0 &amp;&amp; nums&#91;i]>=nums&#91;i+1]){\n        i--;\n    }\n\n    if (i&lt;0){\n        \/\/\u8bf4\u660e\u662f\u6700\u5927\u6392\u5e8f\uff08\u964d\u5e8f\uff09\uff0c\u53cd\u8f6c\u540e\u5373\u4f7f\u6240\u6c42\u7684\u4e0b\u4e00\u4e2a\u6392\u5e8f\uff08\u6700\u5c0f\u7684\uff09\n        <em>reverse<\/em>(nums,0,nums.length-1);\n        return;\n    }\n\n    \/\/2.\u4ece\u540e\u5f80\u524d\u627e\u7b2c\u4e00\u4e2a\u5927\u4e8enums&#91;i]\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u6700\u5c0f\u7684\n    int j = nums.length-1;\n    while (j>i &amp;&amp; nums&#91;j]&lt;=nums&#91;i]){\n        j--;\n    }\n\n    \/\/3.\u8c03\u6362i\uff0cj\u3002\n    <em>swap<\/em>(nums,i,j);\n\n    \/\/4.\u6b64\u65f6nums&#91;i+1]\u5230\u672b\u5c3e\u662f\u964d\u5e8f\uff0c\u53cd\u8f6c\u4e3a\u5347\u5e8f\u4f7f\u5176\u6700\u5c0f\u3002\n    <em>reverse<\/em>(nums,i+1,nums.length-1);\n}\n\n\nprivate static void swap(int&#91;] nums, int i, int j) {\n    int temp = nums&#91;i];\n    nums&#91;i] = nums&#91;j];\n    nums&#91;j] = temp;\n}\n\nprivate static void reverse(int&#91;] nums, int left, int right) {\n    while (left &lt; right) {\n        <em>swap<\/em>(nums, left, right);\n        left++;\n        right--;\n    }\n}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">287.\u5bfb\u627e\u91cd\u590d\u6570<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/find-the-duplicate-number\/\">287. \u5bfb\u627e\u91cd\u590d\u6570<\/a><br>\u6570\u5b66\u8bc1\u660e\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/www.bilibili.com\/video\/BV1qj411w7Vz\/?spm_id_from=333.1007.top_right_bar_window_history.content.click&amp;vd_source=c7c30530a075bcfd068031b47eba229d\">\u5f17\u6d1b\u4f0a\u5fb7\u5faa\u73af\u68c0\u6d4b\u7b97\u6cd5\uff08\u9f9f\u5154\u8d5b\u8dd1\uff09 &#8211; \u5185\u5e55\u4ee3\u7801 &#8211; YouTube \uff08\u5f17\u6d1b\u4f0a\u5fb7\u7b97\u6cd5\uff09_\u54d4\u54e9\u54d4\u54e9_bilibili<\/a><br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int findDuplicate(int&#91;] nums) {<br>    \/\/\u5feb\u6162\u6307\u9488\uff08\u9f9f\u5154\u8d5b\u8dd1 Floyd \u5224\u73af\u7b97\u6cd5\uff09<br>    \/\/nums\u3010i\u3011\u7684\u503c\u5c31\u662f\u4e0b\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u8fd9\u6837\u5c31\u80fd\u6784\u9020\u51fa\u4e00\u4e2a\u94fe\u8868\uff0c\u800c\u5982\u679c\u6709\u91cd\u590d\u5143\u7d20\uff0c\u90e8\u5206\u5143\u7d20\u5c31\u4f1a\u6210\u73af\uff0c\u4f7f\u7528\u5feb\u6162\u6307\u9488\uff08\u9f9f\u5154\u8d5b\u8dd1 Floyd \u5224\u73af\u7b97\u6cd5\uff09\u6765\u627e\u5230\u8fd9\u4e2a\u91cd\u590d\u5143\u7d20<br>    \/\/\u521d\u59cb\u5316\uff0c\u5154\u5b50\u6bcf\u6b21\u591a\u8dd1\u4e00\u6b65<br>    int slow = nums&#91;nums&#91;0]];<br>    int fast = nums&#91;nums&#91;nums&#91;0]]];<br><br><br>    \/\/S1\uff1a\u518d\u73af\u5185\u76f8\u9047\uff08\u4e0d\u4e00\u5b9a\u662f\u91cd\u590d\u70b9\uff09<br>    while(slow!=fast){<br>        slow = nums&#91;slow];<br>        fast = nums&#91;nums&#91;fast]];<br>    }<br><br>    \/\/ Step 2: \u627e\u5230\u5165\u73af\u70b9\uff08\u5373\u91cd\u590d\u7684\u6570\u5b57\uff09<br>    int ptr1 = nums&#91;0];<br>    int ptr2 = slow;<br>    while (ptr1 != ptr2) {<br>        ptr1 = nums&#91;ptr1];<br>        ptr2 = nums&#91;ptr2];<br>    }<br><br>    return ptr1;<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>136.\u53ea\u51fa\u73b0\u4e00\u6b21\u7684\u6570\u5b57 \u4f4d\u8fd0\u7b97\uff0c\u5f02\u6216 \u5f02\u6216\u7279\u70b9\uff1a 136. \u53ea\u51fa\u73b0\u4e00\u6b21\u7684\u6570\u5b57 &#8211; \u529b\u6263\uff08Leet [&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":[],"class_list":["post-813","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/813","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=813"}],"version-history":[{"count":7,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/813\/revisions"}],"predecessor-version":[{"id":1147,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/813\/revisions\/1147"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}