{"id":1260,"date":"2024-04-02T07:34:08","date_gmt":"2024-04-02T07:34:08","guid":{"rendered":"https:\/\/www.gislxz.com\/?p=1260"},"modified":"2024-04-02T07:34:09","modified_gmt":"2024-04-02T07:34:09","slug":"leetcode-49%e5%ad%97%e6%af%8d%e5%bc%82%e4%bd%8d%e8%af%8d","status":"publish","type":"post","link":"https:\/\/www.gislxz.com\/index.php\/2024\/04\/02\/leetcode-49%e5%ad%97%e6%af%8d%e5%bc%82%e4%bd%8d%e8%af%8d\/","title":{"rendered":"Leetcode 49\u5b57\u6bcd\u5f02\u4f4d\u8bcd"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"973\" height=\"850\" src=\"https:\/\/www.gislxz.com\/wp-content\/uploads\/2024\/04\/1712043055-1712043050181.png\" alt=\"\" class=\"wp-image-1261\" srcset=\"https:\/\/www.gislxz.com\/wp-content\/uploads\/2024\/04\/1712043055-1712043050181.png 973w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2024\/04\/1712043055-1712043050181-300x262.png 300w, https:\/\/www.gislxz.com\/wp-content\/uploads\/2024\/04\/1712043055-1712043050181-768x671.png 768w\" sizes=\"auto, (max-width: 973px) 100vw, 973px\" \/><\/figure>\n\n\n\n<p>\u601d\u8def\u8fd8\u662f\u5f88\u5bb9\u6613\u60f3\u5230\u7684\uff0c\u80af\u5b9a\u662f\u54c8\u5e0c\u8868\u8f6cVector\u6570\u7ec4\uff0c\u95ee\u9898\u5728\u54c8\u5e0c\u8868\u7684\u6784\u5efa\u3002\u7b80\u5355\u65b9\u6cd5\u662f\u5bf9\u6bcf\u4e2a\u5355\u8bcd\u6392\u5e8f\u540e\u7684string\u4f5c\u4e3akey\uff0c\u590d\u6742\u4e00\u70b9\u7684\u5c31\u662f\u5bf9\u5b57\u6bcd\u8ba1\u6570\u7136\u540e\u6784\u9020\u54c8\u5e0c\u7684key\uff0c\u4f46\u662fc++\u81ea\u5b9a\u4e49\u54c8\u5e0c\u8fd8\u662f\u633a\u96be\u5199\u7684\uff0c\u8bb0\u5f55\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p>\u6392\u5e8f\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    vector&lt;vector&lt;string>> groupAnagrams(vector&lt;string>&amp; strs) {\n        unordered_map&lt;string, vector&lt;string>> mp;\n        for (string&amp; str: strs) {\n            string key = str;\n            sort(key.begin(), key.end());\n            mp&#91;key].emplace_back(str);\n        }\n        vector&lt;vector&lt;string>> ans;\n        for (auto it = mp.begin(); it != mp.end(); ++it) {\n            ans.emplace_back(it->second);\n        }\n        return ans;\n    }\n};\n\n\u4f5c\u8005\uff1a\u529b\u6263\u5b98\u65b9\u9898\u89e3\n\u94fe\u63a5\uff1ahttps:\/\/leetcode.cn\/problems\/group-anagrams\/solutions\/520469\/zi-mu-yi-wei-ci-fen-zu-by-leetcode-solut-gyoc\/\n\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/code><\/pre>\n\n\n\n<p>\u8ba1\u6570\u6784\u9020\u54c8\u5e0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    vector&lt;vector&lt;string>> groupAnagrams(vector&lt;string>&amp; strs) {\r\n        \/\/ \u81ea\u5b9a\u4e49\u5bf9 array&lt;int, 26> \u7c7b\u578b\u7684\u54c8\u5e0c\u51fd\u6570\r\n        auto arrayHash = &#91;fn = hash&lt;int>{}] (const array&lt;int, 26>&amp; arr) -> size_t {\r\n            return accumulate(arr.begin(), arr.end(), 0u, &#91;&amp;](size_t acc, int num) {\r\n                return (acc &lt;&lt; 1) ^ fn(num);\r\n            });\r\n        };\r\n\r\n        unordered_map&lt;array&lt;int, 26>, vector&lt;string>, decltype(arrayHash)> mp(0, arrayHash);\r\n        for (string&amp; str: strs) {\r\n            array&lt;int, 26> counts{};\r\n            int length = str.length();\r\n            for (int i = 0; i &lt; length; ++i) {\r\n                counts&#91;str&#91;i] - 'a'] ++;\r\n            }\r\n            mp&#91;counts].emplace_back(str);\r\n        }\r\n        vector&lt;vector&lt;string>> ans;\r\n        for (auto it = mp.begin(); it != mp.end(); ++it) {\r\n            ans.emplace_back(it->second);\r\n        }\r\n        return ans;\r\n    }\r\n};\r\n\r\n\u4f5c\u8005\uff1a\u529b\u6263\u5b98\u65b9\u9898\u89e3\r\n\u94fe\u63a5\uff1ahttps:\/\/leetcode.cn\/problems\/group-anagrams\/solutions\/520469\/zi-mu-yi-wei-ci-fen-zu-by-leetcode-solut-gyoc\/\r\n\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\r\n\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def\u8fd8\u662f\u5f88\u5bb9\u6613\u60f3\u5230\u7684\uff0c\u80af\u5b9a\u662f\u54c8\u5e0c\u8868\u8f6cVector\u6570\u7ec4\uff0c\u95ee\u9898\u5728\u54c8\u5e0c\u8868\u7684\u6784\u5efa\u3002\u7b80\u5355\u65b9\u6cd5\u662f\u5bf9\u6bcf\u4e2a\u5355\u8bcd\u6392\u5e8f\u540e\u7684string\u4f5c\u4e3akey\uff0c\u590d\u6742 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1260","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/1260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/comments?post=1260"}],"version-history":[{"count":1,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/1260\/revisions"}],"predecessor-version":[{"id":1262,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/posts\/1260\/revisions\/1262"}],"wp:attachment":[{"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/media?parent=1260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/categories?post=1260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gislxz.com\/index.php\/wp-json\/wp\/v2\/tags?post=1260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}