{"id":799,"date":"2025-03-18T20:33:56","date_gmt":"2025-03-18T12:33:56","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=799"},"modified":"2025-07-13T16:07:09","modified_gmt":"2025-07-13T08:07:09","slug":"hot100-%e5%93%88%e5%b8%8c","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=799","title":{"rendered":"Hot100 \u54c8\u5e0c"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u4e24\u6570\u4e4b\u548c<\/h2>\n\n\n\n<p>&nbsp;<strong>\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u54c8\u5e0c\u6cd5<\/strong>\uff1f<br>\u5f53\u6211\u4eec\u9700\u8981\u67e5\u8be2\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u51fa\u73b0\u8fc7\uff0c\u6216\u8005\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u91cc\u7684\u65f6\u5019\uff0c\u5c31\u8981\u7b2c\u4e00\u65f6\u95f4\u60f3\u5230\u54c8\u5e0c\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u7528map<\/strong><br>\u4f7f\u7528map\uff1akey\uff1a\u5143\u7d20\uff1bvalue\uff1a\u4e0b\u6807<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.util.Arrays;<br>import java.util.HashMap;<br>import java.util.Map;<br><br>\/\/1.\u4e24\u6570\u4e4b\u548c<br>class Solution {<br>    \/\/1.\u66b4\u529bfor\u5faa\u73af<br>    public int[] twoSum1(int[] nums, int target) {<br>            for (int i = 0; i &lt; nums.length; i++) {<br>                for (int j = i+1; j &lt; nums.length; j++) {<br>                    if (nums[i]+nums[j]==target)<br>                        return new int[]{i,j};<br>                }<br>            }<br>            return null;<br>    }<br><br><br>    \/\/2.\u54c8\u5e0c\u8868<br>    \/*<br>    \u4e3a\u4ec0\u4e48\u4f7f\u7528\u54c8\u5e0c\u8868\uff1f<br>    \uff1a\u8981\u5224\u65ad\u8fd9\u4e2a\u5143\u7d20\u662f\u5426\u51fa\u73b0\u8fc7\/\u5728\u8fd9\u4e2a\u96c6\u5408<br>    \u904d\u5386\u5230n\uff0c\u5219\u9700\u8981\u5224\u65adtarget-n\u662f\u5426\u51fa\u73b0\u8fc7<br>    \u4e3a\u4ec0\u4e48\u7528map<br>    \u4f7f\u7528map\uff1akey\uff1a\u5143\u7d20\uff1bvalue\uff1a\u4e0b\u6807<br>     *\/<br>    public int[] twoSum2(int[] nums, int target) {<br>        Map map = new HashMap&lt;Integer,Integer&gt;();<br>        for (int i = 0; i &lt; nums.length; i++) {<br>            int k = target- nums[i];<br>            if (map.containsKey(k))<br>                return new int[]{i, (int) map.get(k)};<br>            else<br>                map.put(nums[i],i);<br>        }<br>        return null;<br>    }<br>}<br><br>public class TwoSum_1 {<br>    public static void main(String[] args) {<br>        int[] arr=new int[]{3,2,4};<br>        int target=6;<br>        Solution solution=new Solution();<br><br>        \/\/1<br>        System.<em>out<\/em>.println(\"@1:\\n\"+ Arrays.<em>toString<\/em>(solution.twoSum1(arr,target)));<br>        \/\/2<br>        System.<em>out<\/em>.println(\"@1:\\n\"+ Arrays.<em>toString<\/em>(solution.twoSum2(arr,target)));<br><br>    }<br>}<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">49.\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u5206\u7ec4<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/group-anagrams\/description\/?envType=study-plan-v2&amp;envId=top-100-liked\">49. \u5b57\u6bcd\u5f02\u4f4d\u8bcd\u5206\u7ec4 &#8211; \u529b\u6263\uff08LeetCode\uff09<\/a><\/p>\n\n\n\n<p>\u8981\u628a\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u5206\u7ec4\uff0c\u5c31\u5f97\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u6765\u5224\u5b9a\u54ea\u4e9b\u5b57\u7b26\u4e32\u662f\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u3002\u7531\u4e8e\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u662f\u7531\u76f8\u540c\u5b57\u6bcd\u7ec4\u6210\u7684\uff0c\u6240\u4ee5\u5bf9\u5b83\u4eec\u7684\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\u540e\uff0c\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u662f\u4e00\u6837\u7684\u3002\u4f8b\u5982\uff0c\u201ceat\u201d\u3001\u201ctea\u201d \u548c \u201cate\u201d \u6392\u5e8f\u540e\u90fd\u53d8\u6210\u4e86 \u201caet\u201d\u3002\u57fa\u4e8e\u8fd9\u4e2a\u7279\u6027\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u6392\u5e8f\u540e\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6807\u8bc6\uff0c\u5c06\u5177\u6709\u76f8\u540c\u6807\u8bc6\u7684\u5b57\u7b26\u4e32\u5f52\u4e3a\u4e00\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.util.*;<br><br>\/\/49.\u5355\u8bcd\u5f02\u4f4d\u8bcd\u5206\u7ec4<br>public class GroupAnagrams_49 {<br>    static class Solution {<br>        public List&lt;List&lt;String&gt;&gt; groupAnagrams(String[] strs) {<br>            \/\/\u7a7a\u4e32\u7279\u6b8a\u5904\u7406<br>            if (strs.length == 0) return new ArrayList&lt;&gt;();<br>            \/\/\u6784\u9020\u6563\u5217\u8868\uff0ckey\u4e3a\u5bf9\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b57\u7b26\u6570\u7ec4\u6392\u5e8f\u540e\u7684\u552f\u4e00\u5b57\u7b26\u4e32\u5e8f\u5217\uff0cvalue\u4e3a\u552f\u4e00\u5bf9\u5e94\u7684list\u7ed3\u679c<br>            Map&lt;String,List&lt;String&gt;&gt; map=new HashMap&lt;&gt;();<br>            for (String s:strs){<br>                char[] chars = s.toCharArray();<br>                \/\/\u5f02\u4f4d\u8bcd\u6392\u5e8f\u540e\u5e8f\u5217\u76f8\u540c<br>                Arrays.<em>sort<\/em>(chars);<br>                \/\/\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u6784\u9020\u4e3a\u5b57\u7b26\u4e32\u4fbf\u4e8e\u6bd4\u8f83,\u4f5c\u4e3akey<br>                String key=String.<em>valueOf<\/em>(chars);<br><br>                if (map.containsKey(key)){<br>                    map.get(key).add(s);<br>                }else {<br>                    List&lt;String&gt; newList = new ArrayList&lt;&gt;();<br>                    newList.add(s);<br>                    map.put(key, newList);<br>                }<br>            }<br>            return  new ArrayList&lt;&gt;(map.values());<br>        }<br>    }<br><br>    public static void main(String[] args) {<br>        String[] strs = {\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"};<br>        Solution solution = new Solution();<br>        List&lt;List&lt;String&gt;&gt; result = solution.groupAnagrams(strs);<br>        for (List&lt;String&gt; group : result) {<br>            System.<em>out<\/em>.println(group);<br>        }<br>    }<br>}<\/pre>\n\n\n\n<p>\u6ce8\uff1a\u8bed\u6cd5\u95ee\u9898\uff1a\u6307\u5b9amap\u6cdb\u578bString,List&lt;String&gt;\u65f6\u8981\u6307\u5b9a\u5728Map\u4f4d\u7f6e\uff0c\u800c\u975e\u540e\u9762\u7684Hashmap\uff1b<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">128.\u6700\u957f\u8fde\u7eed\u5e8f\u5217<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode.cn\/problems\/longest-consecutive-sequence\/description\/?envType=study-plan-v2&amp;envId=top-100-liked\">128. \u6700\u957f\u8fde\u7eed\u5e8f\u5217 &#8211; \u529b\u6263\uff08LeetCode\uff09<\/a><a href=\"https:\/\/leetcode.cn\/u\/leetcode-solution\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u53bb\u91cd\u5904\u7406<\/strong>\uff1a\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u5b58\u5165\u54c8\u5e0c\u8868\u4e2d\uff0c\u81ea\u52a8\u5b8c\u6210\u53bb\u91cd\u3002<\/li>\n\n\n\n<li><strong>\u5bfb\u627e\u5e8f\u5217\u8d77\u70b9<\/strong>\uff1a\u904d\u5386\u54c8\u5e0c\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5224\u65ad\u5176\u662f\u5426\u4e3a\u4e00\u4e2a\u8fde\u7eed\u5e8f\u5217\u7684\u8d77\u70b9\u3002\u5224\u65ad\u65b9\u6cd5\u662f\u68c0\u67e5\u8be5\u5143\u7d20\u51cf 1 \u662f\u5426\u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u6269\u5c55\u5e8f\u5217<\/strong>\uff1a\u5982\u679c\u5f53\u524d\u5143\u7d20\u662f\u5e8f\u5217\u8d77\u70b9\uff0c\u5219\u5c1d\u8bd5\u5411\u53f3\u6269\u5c55\u8be5\u5e8f\u5217\uff0c\u7edf\u8ba1\u5176\u957f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u8bb0\u5f55\u6700\u957f\u957f\u5ea6<\/strong>\uff1a\u5728\u6269\u5c55\u8fc7\u7a0b\u4e2d\uff0c\u8bb0\u5f55\u627e\u5230\u7684\u6700\u957f\u8fde\u7eed\u5e8f\u5217\u957f\u5ea6\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u7528\u54c8\u5e0c\uff1f<\/p>\n\n\n\n<p>\u7528\u4e8e\u5feb\u901f\u67e5\u627enum-1\u5b58\u4e0d\u5b58\u5728\uff01<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int longestConsecutive(int&#91;] nums) {\n        \/\/ \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\u4ee5\u5220\u9664\u91cd\u590d\u5143\u7d20\n        Set&lt;Integer> numSet = new HashSet&lt;>();\n        for (int num : nums) {\n            numSet.add(num);\n        }\n\n        int ans = 0;\n\n        for (int num : numSet) {\n            \/\/ \u53ea\u6709\u5f53num\u662f\u5e8f\u5217\u7684\u8d77\u59cb\u5143\u7d20\u65f6\u624d\u5f00\u59cb\u8ba1\u6570\n            if (!numSet.contains(num - 1)) {\n                int currentNum = num;\n                int currentLen = 1;\n\n                \/\/ \u5411\u53f3\u6269\u5c55\u8ba1\u6570\n                while (numSet.contains(currentNum + 1)) {\n                    currentNum++;\n                    currentLen++;\n                }\n\n                ans = Math.max(ans, currentLen);\n            }\n        }\n\n        return ans;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4e24\u6570\u4e4b\u548c &nbsp;\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u54c8\u5e0c\u6cd5\uff1f\u5f53\u6211\u4eec\u9700\u8981\u67e5\u8be2\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u51fa\u73b0\u8fc7\uff0c\u6216\u8005\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u91cc\u7684\u65f6\u5019 [&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":[74,73],"class_list":["post-799","post","type-post","status-publish","format-standard","hentry","category-3","tag-hot100","tag-73"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/799","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=799"}],"version-history":[{"count":5,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":847,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions\/847"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}