{"id":315,"date":"2019-08-25T21:32:20","date_gmt":"2019-08-25T13:32:20","guid":{"rendered":"https:\/\/kanghaov.com\/?p=315"},"modified":"2019-11-02T15:50:22","modified_gmt":"2019-11-02T07:50:22","slug":"leetcode-%e6%9f%a5%e6%89%be%e8%a1%a8%e7%b1%bb%e7%ae%97%e6%b3%95%e9%a2%98%e7%b2%be%e6%9e%90","status":"publish","type":"post","link":"https:\/\/nemo.cool\/315.html","title":{"rendered":"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790"},"content":{"rendered":"<h2>\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u7cbe\u6790<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>\u67e5\u627e\uff0c\u662f\u4f7f\u7528\u8ba1\u7b97\u673a\u5904\u7406\u95ee\u9898\u65f6\u7684\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u4efb\u52a1\uff0c\u56e0\u6b64\u4e5f\u662f\u7b97\u6cd5\u9762\u8bd5\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u7c7b\u95ee\u9898\u3002\u5f88\u591a\u7b97\u6cd5\u95ee\u9898\u7684\u672c\u8d28\uff0c\u5c31\u662f\u8981\u80fd\u591f\u9ad8\u6548\u4f7f\u7528\u67e5\u627e\u3002LeetCode \u4e2d\u6709\u5f88\u591a\u95ee\u9898\u90fd\u4f1a\u7528\u5230\u96c6\u5408\u548c\u5b57\u5178\uff08C++ \u4e2d\u4e3a set \u548c map\uff0cPython \u4e2d\u4e3a set \u548c dict\uff09\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u4eca\u5929\u6211\u4eec\u5c06\u4f1a\u5bf9 LeetCode \u8fd9\u7c7b\u95ee\u9898\u8fdb\u884c\u603b\u7ed3\uff0c\u6240\u6709\u4ee3\u7801\u91c7\u7528 Python \u5b9e\u73b0\uff0c\u5e76\u53ef\u4ee5\u5728 LeetCode \u91cc AC\u3002<\/p>\n<h3>\u4e24\u7c7b\u67e5\u627e\u95ee\u9898<\/h3>\n<ul>\n<li>\u67e5\u627e\u6709\u65e0\uff1a\u5143\u7d20 &#8216;a&#8217; \u662f\u5426\u5b58\u5728\uff1f\u53ef\u4ee5\u4f7f\u7528 set \u96c6\u5408\u8fd9\u79cd\u6570\u636e\u7ed3\u6784<\/li>\n<li>\u67e5\u627e\u5bf9\u5e94\u5173\u7cfb\uff08\u952e\u503c\u5bf9\u5e94\uff09\uff1a\u5143\u7d20 &#8216;a&#8217; \u51fa\u73b0\u4e86\u51e0\u6b21\uff1f\u53ef\u4ee5\u4f7f\u7528 dict \u5b57\u5178\u8fd9\u79cd\u6570\u636e\u7ed3\u6784<\/li>\n<\/ul>\n<h3>\u96c6\u5408 set \u7684\u4f7f\u7528<\/h3>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4ece <code>set<\/code> \u5f00\u59cb\uff0c\u901a\u8fc7\u4e0b\u5217\u7684\u4e00\u4e9b\u95ee\u9898\u719f\u6089\u5b83\u7684\u4f7f\u7528\u5427\uff01<\/p>\n<h4>\u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6<\/h4>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6570\u7ec4\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u4ea4\u96c6\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums1 = [1,2,2,1], nums2 = [2,2]\n\u8f93\u51fa: [2]\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums1 = [4,9,5], nums2 = [9,4,9,8,4]\n\u8f93\u51fa: [9,4]\n<\/code><\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ul>\n<li>\u8f93\u51fa\u7ed3\u679c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e00\u5b9a\u662f\u552f\u4e00\u7684\u3002<\/li>\n<li>\u6211\u4eec\u53ef\u4ee5\u4e0d\u8003\u8651\u8f93\u51fa\u7ed3\u679c\u7684\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<h5>\u89e3\u7b54<\/h5>\n<h6>\u65b9\u6cd5\u4e00\uff1a\u4e24\u4e2a set<\/h6>\n<p>\u5e7c\u7a1a\u7684\u65b9\u6cd5\u662f\u6839\u636e\u7b2c\u4e00\u4e2a\u6570\u7ec4 <code>nums1<\/code> \u8fed\u4ee3\u5e76\u68c0\u67e5\u6bcf\u4e2a\u503c\u662f\u5426\u5b58\u5728\u5728 <code>nums2<\/code> \u5185\u3002\u5982\u679c\u5b58\u5728\u5c06\u503c\u6dfb\u52a0\u5230\u8f93\u51fa\u3002\u8fd9\u6837\u7684\u65b9\u6cd5\u4f1a\u5bfc\u81f4 <em>O<\/em>(<em>n<\/em>\u00d7<em>m<\/em>) \u7684\u65f6\u95f4\u590d\u6742\u6027\uff0c\u5176\u4e2d <code>n<\/code> \u548c <code>m<\/code> \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<p>\u4e3a\u4e86\u5728\u7ebf\u6027\u65f6\u95f4\u5185\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u4f7f\u7528\u96c6\u5408 <code>set<\/code>\uff0c\u5728 O*(1) \u65f6\u95f4\u590d\u6742\u5ea6\u5b9e\u73b0\u64cd\u4f5c\u3002<\/p>\n<p>\u5176\u601d\u60f3\u662f\u5c06\u4e24\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408 <code>set<\/code>\uff0c\u7136\u540e\u8fed\u4ee3\u8f83\u5c0f\u7684\u96c6\u5408\u68c0\u67e5\u662f\u5426\u5b58\u5728\u5728\u8f83\u5927\u96c6\u5408\u4e2d\u3002\u5e73\u5747\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO<em>(<\/em>n<em>+<\/em>m)\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/13\/5d2e271a1f160.png\" alt=\"1.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/13\/a746cf0dfc2e0.png\" alt=\"2.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/13\/5a0c9e312a276.png\" alt=\"3.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/13\/86954027ec1d5.png\" alt=\"4.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/13\/50667dfa585e6.png\" alt=\"5.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/13\/b25327b415446.png\" alt=\"6.png\" \/><\/p>\n<pre><code class=\"language-python \">class Solution:\n    def set_intersection(self, set1, set2):\n        return [x for x in set1 if x in set2]\n\n    def intersection(self, nums1, nums2):\n        \"\"\"\n        :type nums1: List[int]\n        :type nums2: List[int]\n        :rtype: List[int]\n        \"\"\"\n        set1 = set(nums1)\n        set2 = set(nums2)\n\n        if len(set1) &lt; len(set2):\n            return self.set_intersection(set1, set2)\n        else:\n            return self.set_intersection(set2, set1)\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u7531\u4e8e\u95ee\u9898\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u5173\u5fc3\u5143\u7d20\u7684\u6709\u65e0\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>set<\/code> \u8fd9\u4e2a\u7ed3\u6784\u3002\u9996\u5148\u5c06 <code>nums1<\/code> \u7684\u6240\u6709\u6570\u636e\u5b58\u5165 <code>set<\/code> \u4e2d\uff0c\u518d\u5c06 <code>nums2<\/code> \u7684\u6240\u6709\u6570\u7ec4\u4e5f\u5b58\u5165 <code>set<\/code> \u4e2d\uff0c\u6700\u540e\u6c42\u5e76\u96c6\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def intersection(self, nums1, nums2):\n        \"\"\"\n        :type nums1: List[int]\n        :type nums2: List[int]\n        :rtype: List[int]\n        \"\"\"\n        a = set(nums1)\n        b = set(nums2)\n        c = a &amp; b\n        return list(c)\n<\/code><\/pre>\n<h4>\u5feb\u4e50\u6570<\/h4>\n<p>\u7f16\u5199\u4e00\u4e2a\u7b97\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u4e0d\u662f\u201c\u5feb\u4e50\u6570\u201d\u3002<\/p>\n<p>\u4e00\u4e2a\u201c\u5feb\u4e50\u6570\u201d\u5b9a\u4e49\u4e3a\uff1a\u5bf9\u4e8e\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u6bcf\u4e00\u6b21\u5c06\u8be5\u6570\u66ff\u6362\u4e3a\u5b83\u6bcf\u4e2a\u4f4d\u7f6e\u4e0a\u7684\u6570\u5b57\u7684\u5e73\u65b9\u548c\uff0c\u7136\u540e\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u76f4\u5230\u8fd9\u4e2a\u6570\u53d8\u4e3a 1\uff0c\u4e5f\u53ef\u80fd\u662f\u65e0\u9650\u5faa\u73af\u4f46\u59cb\u7ec8\u53d8\u4e0d\u5230 1\u3002\u5982\u679c\u53ef\u4ee5\u53d8\u4e3a 1\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6570\u5c31\u662f\u5feb\u4e50\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: 19\n\u8f93\u51fa: true\n\u89e3\u91ca:\n12 + 92 = 82\n82 + 22 = 68\n62 + 82 = 100\n12 + 02 + 02 = 1\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<p><strong>\u9012\u5f52\u89e3\u6cd5<\/strong><\/p>\n<pre><code class=\"language-python \">class Solution:\n    def isHappy(self, n: int) -&gt; bool:\n        return self.isHappy(sum(int(i) ** 2 for i in str(n))) if n &gt; 4 else n == 1\n<\/code><\/pre>\n<ul>\n<li>\u4e0d\u662f\u5feb\u4e50\u6570\u7684\u6570\u79f0\u4e3a\u4e0d\u5feb\u4e50\u6570(unhappy number)\uff0c\u6240\u6709\u4e0d\u5feb\u4e50\u6570\u7684\u6570\u4f4d\u5e73\u65b9\u548c\u8ba1\u7b97\uff0c\u6700\u540e\u90fd\u4f1a\u8fdb\u5165 4 \u2192 16 \u2192 37 \u2192 58 \u2192 89 \u2192 145 \u2192 42 \u2192 20 \u2192 4 \u7684\u5faa\u73af\u4e2d<\/p>\n<\/li>\n<li>\n<p>\u5df2\u77e5\u89c4\u5f8b\uff1a [1 ~ 4] \u4e2d\u53ea\u6709 1 \u662f\u5feb\u4e50\u6570\uff0c[5 ~ \u221e] \u7684\u6570\u5b57\u8981\u4e48\u56de\u5f52\u5230 1 \u8981\u4e48\u56de\u5f52\u5230 4 \u6216 3<\/p>\n<\/li>\n<li>\u56e0\u6b64\u4ec5\u9700\u5728 n > 4 \u65f6\u8c03\u7528\u9012\u5f52<\/li>\n<\/ul>\n<p><strong>\u7ec6\u8282\u8865\u5145<\/strong><\/p>\n<ul>\n<li>\u5c3e\u9012\u5f52\uff1a\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u4e2d\u6240\u6709\u9012\u5f52\u5f62\u5f0f\u7684\u8c03\u7528\u90fd\u51fa\u73b0\u5728\u51fd\u6570\u7684\u672b\u5c3e\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u9012\u5f52\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u7684\u3002\u5f53\u9012\u5f52\u8c03\u7528\u662f\u6574\u4e2a\u51fd\u6570\u4f53\u4e2d\u6700\u540e\u6267\u884c\u7684\u8bed\u53e5\u4e14\u5b83\u7684\u8fd4\u56de\u503c\u4e0d\u5c5e\u4e8e\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u8fd9\u4e2a\u9012\u5f52\u8c03\u7528\u5c31\u662f\u5c3e\u9012\u5f52\u3002\u5c3e\u9012\u5f52\u51fd\u6570\u7684\u7279\u70b9\u662f\u5728\u56de\u5f52\u8fc7\u7a0b\u4e2d\u4e0d\u7528\u505a\u4efb\u4f55\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u7279\u6027\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u73b0\u4ee3\u7684\u7f16\u8bd1\u5668\u4f1a\u5229\u7528\u8fd9\u79cd\u7279\u70b9\u81ea\u52a8\u751f\u6210\u4f18\u5316\u7684\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><strong>\u96c6\u5408\u89e3\u6cd5<\/strong><\/p>\n<ul>\n<li>\u4ee5\u4e0a\u89e3\u6cd5\u7684\u89c4\u5f8b\u6bd4\u8f83\u96be\u60f3\u5230\u7684\uff0c\u6b63\u5e38\u89e3\u6cd5\u662f\u5224\u65ad n \u662f\u5426\u4f1a\u8fdb\u5165\u5faa\u73af\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python \">class Solution:\n    def isHappy(self, n: int) -&gt; bool:\n        seen = {1}\n        while n not in seen:\n            seen.add(n)\n            n = sum(int(i) ** 2 for i in str(n))\n        return n == 1\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u5224\u65ad\u51fa\u73b0\u7684\u6570\u4e4b\u524d\u6709\u6ca1\u6709\u51fa\u73b0\u8fc7\uff0c\u51fa\u73b0\u8fc7\u5c31\u4f1a\u4ea7\u751f\u5faa\u73af\uff0c\u5c31\u4e0d\u662f\u5feb\u4e50\u6570\u3002\u53ef\u4ee5\u7528\u96c6\u5408 <code>set<\/code> \u6765\u8bb0\u5f55\u4e4b\u524d\u51fa\u73b0\u7684\u6570\u5b57\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def isHappy(self, n):\n        \"\"\"\n        :type n: int\n        :rtype: bool\n        \"\"\"\n\n        ss = set()\n        while True:\n            if n == 1:\n                return True\n            total = 0\n            while n:\n                total += (n % 10) * (n %10)\n                n = n \/\/ 10\n            if total in ss:\n                return False\n\n            ss.add(total)\n            n = total\n<\/code><\/pre>\n<h4>\u5b58\u5728\u91cd\u590d\u5143\u7d20<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5224\u65ad\u662f\u5426\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p>\u5982\u679c\u4efb\u4f55\u503c\u5728\u6570\u7ec4\u4e2d\u51fa\u73b0\u81f3\u5c11\u4e24\u6b21\uff0c\u51fd\u6570\u8fd4\u56de true\u3002\u5982\u679c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e0d\u76f8\u540c\uff0c\u5219\u8fd4\u56de false\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: [1,2,3,1]\n\u8f93\u51fa: true\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: [1,2,3,4]\n\u8f93\u51fa: false\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: [1,1,1,3,3,4,3,2,4,2]\n\u8f93\u51fa: true\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<pre><code class=\"language-python \">class Solution:\n    def containsDuplicate(self, nums: List[int]) -&gt; bool:\n        set1 = set(nums)\n        if len(set1) != len(nums):\n            return 'true'\n        else:\n            return 'false'\n<\/code><\/pre>\n<p>\u6d4b\u8bd5\u901a\u8fc7\uff0c\u4f46\u662f\u63d0\u4ea4\u62a5\u9519\u3002\u4e3a\u5565\uff1f<\/p>\n<p>\u56e0\u4e3a\uff01\u4e0d\u80fd\u6309\u9898\u76ee\u4e2d\u7684\u6765\uff01\uff01<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsDuplicate(self, nums: List[int]) -&gt; bool:\n        set1 = set(nums)\n        if len(set1) != len(nums):\n            return True\n        else:\n            return False\n<\/code><\/pre>\n<p><strong>\u89e3\u6cd51\uff1a\u96c6\u5408\u6cd5<\/strong><br \/>\n\u5224\u65ad\u539f\u6570\u7ec4\u548c\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\u76f8\u4e0d\u76f8\u7b49\uff0c\u4e00\u884c\u89e3\u51b3\uff1a<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsDuplicate(self, nums: List[int]) -&gt; bool:\n        return len((set(nums))) != len(nums)\n\n<\/code><\/pre>\n<p><strong>\u89e3\u6cd52\uff1a\u54c8\u5e0c\u8868<\/strong><\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsDuplicate(self, nums: List[int]) -&gt; bool:\n        dic = {}\n        for i in nums:\n            if dic.get(i):\n                return True\n            dic[i] = 1\n        return False\n<\/code><\/pre>\n<p><strong>\u89e3\u6cd53\uff1a\u6392\u5e8f\u6cd5<\/strong><\/p>\n<p>\u6392\u5e8f\u4e4b\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5fc5\u76f8\u90bb\uff1a<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsDuplicate(self, nums: List[int]) -&gt; bool:\n        nums.sort()\n        for i in range(len(nums)-1):\n            if nums[i+1] == nums[i]:\n                return True\n        return False\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u4f7f\u7528 <code>set<\/code> \u96c6\u5408\u5b58\u50a8\u524d\u9762\u904d\u5386\u8fc7\u7684\u5143\u7d20\uff0c\u5bf9\u4e8e\u8981\u6bcf\u4e2a\u5143\u7d20 <code>num[i]<\/code>\uff0c\u5224\u65ad\u662f\u5426\u5728\u524d\u9762\u7684 <code>recode<\/code> \u4e2d\uff0c\u5982\u679c\u5b58\u5728\uff0c\u8fd4\u56de <code>True<\/code>\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsDuplicate(self, nums):\n        \"\"\"\n        :type nums: List[int]\n        :rtype: bool\n        \"\"\"\n\n        n = len(nums)\n        if n &lt;= 1:\n            return False\n\n        recode = set()\n        recode.add(nums[0])\n        for i in range(1, n):\n            if nums[i] in recode:\n                return True\n            recode.add(nums[i])\n        return False\n<\/code><\/pre>\n<h3>\u5b57\u5178 dict \u7684\u4f7f\u7528<\/h3>\n<p>\u4e00\u4e9b\u8f83\u4e3a\u5178\u578b\u7684\u5b57\u5178\u4f7f\u7528\u6848\u4f8b\u3002<\/p>\n<h4>\u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6 II<\/h4>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6570\u7ec4\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u4ea4\u96c6\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums1 = [1,2,2,1], nums2 = [2,2]\n\u8f93\u51fa: [2,2]\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums1 = [4,9,5], nums2 = [9,4,9,8,4]\n\u8f93\u51fa: [4,9]\n\n<\/code><\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u8f93\u51fa\u7ed3\u679c\u4e2d\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5e94\u4e0e\u5143\u7d20\u5728\u4e24\u4e2a\u6570\u7ec4\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u4e00\u81f4\u3002<\/li>\n<li>\u6211\u4eec\u53ef\u4ee5\u4e0d\u8003\u8651\u8f93\u51fa\u7ed3\u679c\u7684\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<p><strong>\u8fdb\u9636:<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u7ed9\u5b9a\u7684\u6570\u7ec4\u5df2\u7ecf\u6392\u597d\u5e8f\u5462\uff1f\u4f60\u5c06\u5982\u4f55\u4f18\u5316\u4f60\u7684\u7b97\u6cd5\uff1f<\/li>\n<li>\u5982\u679c <em>nums1<\/em> \u7684\u5927\u5c0f\u6bd4 <em>nums2<\/em> \u5c0f\u5f88\u591a\uff0c\u54ea\u79cd\u65b9\u6cd5\u66f4\u4f18\uff1f<\/li>\n<li>\u5982\u679c <em>nums2<\/em> \u7684\u5143\u7d20\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\uff0c\u78c1\u76d8\u5185\u5b58\u662f\u6709\u9650\u7684\uff0c\u5e76\u4e14\u4f60\u4e0d\u80fd\u4e00\u6b21\u52a0\u8f7d\u6240\u6709\u7684\u5143\u7d20\u5230\u5185\u5b58\u4e2d\uff0c\u4f60\u8be5\u600e\u4e48\u529e\uff1f<\/li>\n<\/ul>\n<h5>\u89e3\u7b54<\/h5>\n<p><strong>\u601d\u8def<\/strong><\/p>\n<ul>\n<li>\u5efa\u7acb\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u4e3anums1\u7684\u6570\uff0c\u503c\u4e3a\u8fd9\u4e2a\u6570\u51fa\u73b0\u7684\u6b21\u6570<\/li>\n<li>\u904d\u5386num2,\u5f53num2\u7684\u6570\u51fa\u73b0\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u5c06\u5b83\u6dfb\u52a0\u5230\u7b54\u6848\u91cc\uff0c\u540c\u65f6\u66f4\u65b0\u54c8\u5e0c\u8868\uff0c\u8ba9\u5176\u51fa\u73b0\u6b21\u6570\u51cf\u4e00\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-python \">from collections import defaultdict\nclass Solution:\n    def intersect(self, nums1: List[int], nums2: List[int]) -&gt; List[int]:\n        dic, ans = defaultdict(int), list()\n        for i in nums1: dic[i] += 1\n        for i in nums2:\n            if dic[i] != 0:\n                ans.append(i)\n                dic[i] -= 1\n        return ans\n\n<\/code><\/pre>\n<ul>\n<li><code>defaultdict(int)<\/code>\u9879\u5bf9\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u952e\u4f1a\u521d\u59cb\u5316\u4e00\u4e2a0\u7684\u503c<\/li>\n<li>\u8fd9\u9898\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>Counter<\/code>\u76f4\u63a5\u7edf\u8ba1\u51fa\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u548c\u624b\u52a8\u904d\u5386\u662f\u4e00\u6837\u7684\u3002<\/li>\n<\/ul>\n<p><strong>\u590d\u6742\u5ea6\u5206\u6790<\/strong><\/p>\n<p>\u5047\u8bbenum1\u6709M\u9879\uff0cnum2\u6709m\u9879\uff0c\u5176\u4e2dM>m\uff0c\u5219<\/p>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6O<em>(<\/em>M<em>+<\/em>m)<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6O<em>(<\/em>m)<\/li>\n<\/ul>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u6211\u4eec\u5bf9\u6bd4<a href=\"https:\/\/leetcode-cn.com\/explore\/featured\/card\/all-about-lockup-table\/236\/learn-to-use-set\/977\/\">\u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6<\/a>\uff0c\u53d1\u73b0\u5728\u8fd9\u4e2a\u95ee\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u591a\u52a0\u4e86\u8bb0\u5f55\u5143\u7d20\u4e2a\u6570\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178 <code>dict<\/code>\u5b9e\u73b0\u5b83\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def intersect(self, nums1, nums2):\n        \"\"\"\n        :type nums1: List[int]\n        :type nums2: List[int]\n        :rtype: List[int]\n        \"\"\"\n\n        m = len(nums1)\n        n = len(nums2)\n\n        if m == 0 or n == 0:\n            return []\n\n\n        dicts = {}\n        for i in nums1:\n            if i in dicts:\n                dicts[i] += 1\n            else:\n                dicts[i] = 1\n\n        ret = []\n        for j in nums2:\n            if j in dicts and dicts[j] &gt; 0:\n                ret.append(j)\n                dicts[j] -= 1\n\n        return ret\n<\/code><\/pre>\n<h4>\u6709\u6548\u7684\u5b57\u6bcd\u5f02\u4f4d\u8bcd<\/h4>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32 <em>s<\/em> \u548c <em>t<\/em> \uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad <em>t<\/em> \u662f\u5426\u662f <em>s<\/em> \u7684\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: s = \"anagram\", t = \"nagaram\"\n\u8f93\u51fa: true\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: s = \"rat\", t = \"car\"\n\u8f93\u51fa: false\n\n<\/code><\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><br \/>\n\u4f60\u53ef\u4ee5\u5047\u8bbe\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u8fdb\u9636:<\/strong><br \/>\n\u5982\u679c\u8f93\u5165\u5b57\u7b26\u4e32\u5305\u542b unicode \u5b57\u7b26\u600e\u4e48\u529e\uff1f\u4f60\u80fd\u5426\u8c03\u6574\u4f60\u7684\u89e3\u6cd5\u6765\u5e94\u5bf9\u8fd9\u79cd\u60c5\u51b5\uff1f<\/p>\n<h5>\u89e3\u7b54<\/h5>\n<pre><code class=\"language-python \">from collections import Counter\nclass Solution:\n    def isAnagram(self, s: str, t: str) -&gt; bool:\n        return Counter(s) == Counter(t)\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u53ea\u8981 <code>t<\/code> \u4e2d\u548c <code>s<\/code> \u4e2d\u5b57\u7b26\u90fd\u4e00\u6837\u4e14\u6570\u76ee\u90fd\u4e00\u6837\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u4e86\u3002Python \u5b9e\u73b0\u7684\u8bdd\u53ef\u4ee5\u91c7\u7528 <code>collections<\/code>\u3002<code>Counter<\/code> \u8fd9\u4e2a\u51fd\u6570\u5b9e\u73b0\u5bf9\u5404\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<pre><code class=\"language-python \">from collections import Counter\nclass Solution:\n    def isAnagram(self, s, t):\n        \"\"\"\n        :type s: str\n        :type t: str\n        :rtype: bool\n        \"\"\"\n        c1 = Counter(s)\n        c2 = Counter(t)\n        if (c1 == c2):\n            return True\n        return False\n<\/code><\/pre>\n<h4>\u540c\u6784\u5b57\u7b26\u4e32<\/h4>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32 <strong><em>s<\/em><\/strong> \u548c <strong>t<\/strong>\uff0c\u5224\u65ad\u5b83\u4eec\u662f\u5426\u662f\u540c\u6784\u7684\u3002<\/p>\n<p>\u5982\u679c <strong><em>s<\/em><\/strong> \u4e2d\u7684\u5b57\u7b26\u53ef\u4ee5\u88ab\u66ff\u6362\u5f97\u5230 <strong>t<\/strong> \uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u540c\u6784\u7684\u3002<\/p>\n<p>\u6240\u6709\u51fa\u73b0\u7684\u5b57\u7b26\u90fd\u5fc5\u987b\u7528\u53e6\u4e00\u4e2a\u5b57\u7b26\u66ff\u6362\uff0c\u540c\u65f6\u4fdd\u7559\u5b57\u7b26\u7684\u987a\u5e8f\u3002\u4e24\u4e2a\u5b57\u7b26\u4e0d\u80fd\u6620\u5c04\u5230\u540c\u4e00\u4e2a\u5b57\u7b26\u4e0a\uff0c\u4f46\u5b57\u7b26\u53ef\u4ee5\u6620\u5c04\u81ea\u5df1\u672c\u8eab\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: s = \"egg\", t = \"add\"\n\u8f93\u51fa: true\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: s = \"foo\", t = \"bar\"\n\u8f93\u51fa: false\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: s = \"paper\", t = \"title\"\n\u8f93\u51fa: true\n<\/code><\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><br \/>\n\u4f60\u53ef\u4ee5\u5047\u8bbe <strong><em>s<\/em><\/strong> \u548c <strong>t<\/strong> \u5177\u6709\u76f8\u540c\u7684\u957f\u5ea6\u3002<\/p>\n<h5>\u89e3\u7b54<\/h5>\n<p><strong>\u9898\u89e3<\/strong><\/p>\n<pre><code class=\"language-python \">class Solution:\n    def isIsomorphic(self, s: str, t: str) -&gt; bool:\n        return [*map(s.index, s)] == [*map(t.index, t)]\n<\/code><\/pre>\n<ul>\n<li>\u540c\u6784\u4ee3\u8868\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e\u4e0a\u5b57\u7b26\u5728\u81ea\u8eab\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u76f8\u540c<\/li>\n<\/ul>\n<p><strong>\u7ec6\u8282\u8865\u5145<\/strong><\/p>\n<ul>\n<li>str \u7c7b\u578b\u6570\u636e\u62e5\u6709\u5185\u7f6e\u51fd\u6570 index\uff0c\u8f93\u5165\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728 str \u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u82e5\u4e0d\u5b58\u5728\u5219\u62a5\u9519<\/p>\n<\/li>\n<li>\n<p>map(\u51fd\u6570\uff0c\u53ef\u8fed\u4ee3\u5bf9\u8c61) \u5c06\u4f1a\u5bf9\uff08\u53c2\u65702\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff09\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fd0\u7528\uff08\u53c2\u65701\uff1a\u51fd\u6570\uff09\u5e76\u5c06\u7ed3\u679c\u6309\u987a\u5e8f\u50a8\u5b58\u5728\u4e00\u4e2a\u8fed\u4ee3\u5668\u4e2d\uff0c\u8fd4\u56de\u8fd9\u4e2a\u8fed\u4ee3\u5668<\/p>\n<\/li>\n<li>\u4f7f\u7528 <code>[*\u2026\u2026]<\/code> \u53ef\u5bf9\u5bf9\u8c61\u89e3\u5305\uff0c\u672c\u9898\u4e2d <code>[*map\u2026\u2026]<\/code> \u7b49\u6548\u4e8e <code>list(map\u2026\u2026)<\/code><\/li>\n<\/ul>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178 <code>dict<\/code> \u6765\u8bb0\u4f4f\u8fd9\u4e9b\u5b57\u7b26\u5bf9\u662f\u4e00\u4e2a\u5f88\u65b9\u4fbf\u7684\u505a\u6cd5\u3002\u5728\u8fd9\u91cc\u9762\u6211\u7528\u4e86\u4e24\u4e2a\u5b57\u5178 <code>dict<\/code>\uff0c\u4e00\u4e2a\u5b57\u5178 <code>hashmap<\/code> \u7528\u6765\u8bb0 <code>s<\/code> \u7684\u5b57\u7b26\u5230 <code>t<\/code> \u7684\u6620\u5c04\uff0c\u53e6\u4e00\u4e2a\u5b57\u5178 <code>ismap<\/code> \u7528\u6765\u8bb0\u5f55 <code>t<\/code> \u7684\u5b57\u7b26\u5230 <code>s<\/code> \u7684\u6620\u5c04\uff0c\u7528\u4e8e\u5224\u65ad <code>t<\/code> \u4e2d\u7684\u4e24\u4e2a\u5b57\u7b26\u4e0d\u80fd\u7531 <code>s<\/code> \u4e2d\u540c\u4e00\u4e2a\u5b57\u7b26\u6620\u5c04\u800c\u6765\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def isIsomorphic(self, s, t):\n        \"\"\"\n        :type s: str\n        :type t: str\n        :rtype: bool\n        \"\"\"\n\n        hashmap = {}\n        ismap = {}\n        for i in range(len(s)):\n            if s[i] in hashmap:\n                if hashmap[s[i]] != t[i]:\n                    return False\n            else:\n                if t[i] in ismap:\n                    return False\n                hashmap[s[i]] = t[i]\n                ismap[t[i]] = True\n        return True\n<\/code><\/pre>\n<h4>\u6839\u636e\u5b57\u7b26\u51fa\u73b0\u9891\u7387\u6392\u5e8f<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8bf7\u5c06\u5b57\u7b26\u4e32\u91cc\u7684\u5b57\u7b26\u6309\u7167\u51fa\u73b0\u7684\u9891\u7387\u964d\u5e8f\u6392\u5217\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165:\n\"tree\"\n\n\u8f93\u51fa:\n\"eert\"\n\n\u89e3\u91ca:\n'e'\u51fa\u73b0\u4e24\u6b21\uff0c'r'\u548c't'\u90fd\u53ea\u51fa\u73b0\u4e00\u6b21\u3002\n\u56e0\u6b64'e'\u5fc5\u987b\u51fa\u73b0\u5728'r'\u548c't'\u4e4b\u524d\u3002\u6b64\u5916\uff0c\"eetr\"\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7b54\u6848\u3002\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165:\n\"cccaaa\"\n\n\u8f93\u51fa:\n\"cccaaa\"\n\n\u89e3\u91ca:\n'c'\u548c'a'\u90fd\u51fa\u73b0\u4e09\u6b21\u3002\u6b64\u5916\uff0c\"aaaccc\"\u4e5f\u662f\u6709\u6548\u7684\u7b54\u6848\u3002\n\u6ce8\u610f\"cacaca\"\u662f\u4e0d\u6b63\u786e\u7684\uff0c\u56e0\u4e3a\u76f8\u540c\u7684\u5b57\u6bcd\u5fc5\u987b\u653e\u5728\u4e00\u8d77\u3002\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165:\n\"Aabb\"\n\n\u8f93\u51fa:\n\"bbAa\"\n\n\u89e3\u91ca:\n\u6b64\u5916\uff0c\"bbaA\"\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7b54\u6848\uff0c\u4f46\"Aabb\"\u662f\u4e0d\u6b63\u786e\u7684\u3002\n\u6ce8\u610f'A'\u548c'a'\u88ab\u8ba4\u4e3a\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u5b57\u7b26\u3002\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<pre><code class=\"language-python \">class Solution(object):\n    def frequencySort(self, s):\n        \"\"\"\n        :type s: str\n        :rtype: str\n        \"\"\"\n        res = ''\n        map = {}\n        for i in s :\n            if i not in map:\n                map[i] = 1\n            else :\n                map[i] += 1\n        l = sorted(map.items(), key=lambda x: x[1], reverse=True)\n        for v , k in l:\n            res += v*k\n        return res\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u901a\u8fc7\u4e00\u4e2a\u5b57\u5178 <code>dict<\/code> \u5b58\u50a8\u6240\u6709\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u56e0\u4e3a\u67d0\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u4e0d\u53ef\u80fd\u8d85\u8fc7 <code>s<\/code> \u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u6bcf\u4e2a\u5b57\u7b26\u6839\u636e\u5176\u51fa\u73b0\u6b21\u6570\u653e\u5165\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\uff0c\u90a3\u4e48\u6700\u540e\u6211\u4eec\u53ea\u8981\u4ece\u540e\u5f80\u524d\u904d\u5386\u6570\u7ec4\u6240\u6709\u4f4d\u7f6e\uff0c\u5c06\u4e0d\u4e3a\u7a7a\u7684\u4f4d\u7f6e\u7684\u5b57\u7b26\u4e32\u52a0\u5165\u7ed3\u679c <code>ret<\/code> \u4e2d\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-python \">from collections import Counter\nclass Solution:\n    def frequencySort(self, s):\n        \"\"\"\n        :type s: str\n        :rtype: str\n        \"\"\"\n        n = len(s)\n        sc = dict(Counter(s))\n        ll = ['' for i in range(0, n+1)]\n        isvisited = set()\n        ret = \"\"\n        for key,value in sc.items():\n                ll[value] += key * value\n        for i in range(n, -1, -1):\n            if ll[i] != '':\n                ret += ll[i]\n        return ret;\n\n<\/code><\/pre>\n<h3>\u67e5\u627e\u8868\u4e0e\u6c42\u548c\u95ee\u9898<\/h3>\n<p>\u7ae0\u8282\u6536\u5f55\u4e86\u4e00\u4e9b\u7ecf\u5178\u7684\u5173\u4e8e\u67e5\u627e\u8868\u76f8\u5173\u7684\u6c42\u548c\u7cfb\u5217\u95ee\u9898\u3002<\/p>\n<h4>\u4e24\u6570\u4e4b\u548c<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>nums<\/code> \u548c\u4e00\u4e2a\u76ee\u6807\u503c <code>target<\/code>\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa\u548c\u4e3a\u76ee\u6807\u503c\u7684\u90a3 <strong>\u4e24\u4e2a<\/strong> \u6574\u6570\uff0c\u5e76\u8fd4\u56de\u4ed6\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u4f60\u4e0d\u80fd\u91cd\u590d\u5229\u7528\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u540c\u6837\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><code class=\"\">\u7ed9\u5b9a nums = [2, 7, 11, 15], target = 9\n\n\u56e0\u4e3a nums[0] + nums[1] = 2 + 7 = 9\n\u6240\u4ee5\u8fd4\u56de [0, 1]\n<\/code><\/pre>\n<pre><code class=\"language-python \">class Solution:\n  def twoSum(self, nums, target):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \"\"\"\n        answer = []\n        for left_index in range(len(nums)):\n            right = target - nums[left_index]\n            if right in nums[left_index+1:]:\n                nums_right = nums[left_index+1:]\n                right_index = nums_right.index(right)+left_index+1\n                answer.extend([left_index, right_index])\n                break\n        return answer\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p><strong>\u66b4\u529b\u89e3\u6cd5<\/strong><\/p>\n<p>\u904d\u5386\u6240\u6709\u6570\u636e\u5bf9\uff0c\u5224\u65ad\u662f\u5426\u7b49\u4e8e <code>target<\/code>\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5ea6 O(n^2)\u3002<\/p>\n<p><strong>\u53cc\u7d22\u5f15\u5bf9\u649e<\/strong><\/p>\n<p>\u5148\u6392\u5e8f\u540e\uff0c\u540e\u4f7f\u7528\u53cc\u7d22\u5f15\u5bf9\u649e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1aO(n log n) + O(n) = O(n log n)\uff0c \u53ef\u4ee5\u8bd5\u4e00\u8bd5\uff0c\u4e5f\u662f\u53ef\u4ee5 AC \u7684\u3002<\/p>\n<p><strong>\u4f7f\u7528\u67e5\u627e\u8868<\/strong><\/p>\n<p>\u5c06\u6240\u6709\u5143\u7d20\u653e\u5165\u67e5\u627e\u8868\uff0c\u4e4b\u540e\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u5143\u7d20 <code>a<\/code>\uff0c\u67e5\u627e <code>target - a<\/code> \u662f\u5426\u5b58\u5728\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def twoSum(self, nums, target):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \"\"\"\n\n        n = len(nums)\n        if n &lt; 2:\n            return []\n\n        dict1 = dict();\n        for i in range(n):\n            other = target - nums[i]\n            if other in dict1:\n                return [dict1[other], i]\n            dict1[nums[i]] = i\n\n        return []\n\n<\/code><\/pre>\n<h4>\u4e09\u6570\u4e4b\u548c<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b <em>n<\/em> \u4e2a\u6574\u6570\u7684\u6570\u7ec4 <code>nums<\/code>\uff0c\u5224\u65ad <code>nums<\/code> \u4e2d\u662f\u5426\u5b58\u5728\u4e09\u4e2a\u5143\u7d20 <em>a\uff0cb\uff0cc \uff0c<\/em>\u4f7f\u5f97 <em>a + b + c =<\/em> 0 \uff1f\u627e\u51fa\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u4e14\u4e0d\u91cd\u590d\u7684\u4e09\u5143\u7ec4\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u7b54\u6848\u4e2d\u4e0d\u53ef\u4ee5\u5305\u542b\u91cd\u590d\u7684\u4e09\u5143\u7ec4\u3002<\/p>\n<pre><code class=\"\">\u4f8b\u5982, \u7ed9\u5b9a\u6570\u7ec4 nums = [-1, 0, 1, 2, -1, -4]\uff0c\n\n\u6ee1\u8db3\u8981\u6c42\u7684\u4e09\u5143\u7ec4\u96c6\u5408\u4e3a\uff1a\n[\n  [-1, 0, 1],\n  [-1, -1, 2]\n]\n\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<pre><code class=\"language-python \">class Solution:\n    def threeSum(self, nums: [int]) -&gt; [[int]]:\n        nums.sort()\n        res, k = [], 0\n        for k in range(len(nums) - 2):\n            if nums[k] &gt; 0: break # because of j &gt; i &gt; k.\n            if k &gt; 0 and nums[k] == nums[k - 1]: continue # skip.\n            i, j = k + 1, len(nums) - 1\n            while i &lt; j:\n                s = nums[k] + nums[i] + nums[j]\n                if s &lt; 0:\n                    i += 1\n                    while i &lt; j and nums[i] == nums[i - 1]: i += 1\n                elif s &gt; 0:\n                    j -= 1\n                    while i &lt; j and nums[j] == nums[j + 1]: j -= 1\n                else:\n                    res.append([nums[k], nums[i], nums[j]])\n                    i += 1\n                    j -= 1\n                    while i &lt; j and nums[i] == nums[i - 1]: i += 1\n                    while i &lt; j and nums[j] == nums[j + 1]: j -= 1\n        return res\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u53ef\u4ee5\u8f6c\u5316\u6210\u4e0a\u4e00\u9898\u7684\u4e24\u6570\u4e4b\u548c\u7684\u95ee\u9898\uff0c<code>a + b = -c<\/code>\uff0c\u6539\u52a8\u4e00\u4e0b<a href=\"https:\/\/leetcode-cn.com\/explore\/featured\/card\/all-about-lockup-table\/238\/lookup-table-related-sum-questions\/992\/\">\u4e24\u6570\u4e4b\u548c<\/a>\u7684 <code>twoSum<\/code> \u51fd\u6570\u5373\u53ef\u5b9e\u73b0\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n\n    def twoSum(self, nums, target, l, r):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \"\"\"\n\n        n = len(nums[l: r+1])\n        if n &lt; 2:\n            return set()\n\n        start = nums[l - 1]\n        dict1 = dict()\n        ret = set()\n        for i in range(l, r+1):\n            other = target - nums[i]\n            if other in dict1:\n                ret.add((start, other, nums[i]))\n            dict1[nums[i]] = i\n\n        return ret\n\n    def threeSum(self, nums):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \"\"\"\n\n        n = len(nums)\n        if n &lt; 3:\n            return []\n\n        nums.sort()\n\n        ret = set()\n        for i in range(n):\n            # nums[i] \u4e3a\u6b63\u6570\uff0c\u8868\u793a\u540e\u9762\u4e0d\u53ef\u80fd\u6709\u4e24\u6570\u4e4b\u540e\u4e3a\u8d1f\n            if nums[i] &gt; 0:\n                break\n            if (i &gt;= 1 and nums[i] == nums[i-1]):\n                continue\n            ll = self.twoSum(nums, -nums[i], i+1, n-1)\n            if (ll == set()):\n                continue\n            ret = ret | ll\n        return list(ret)\n<\/code><\/pre>\n<h4>\u56db\u6570\u4e4b\u548c<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b <em>n<\/em> \u4e2a\u6574\u6570\u7684\u6570\u7ec4 <code>nums<\/code> \u548c\u4e00\u4e2a\u76ee\u6807\u503c <code>target<\/code>\uff0c\u5224\u65ad <code>nums<\/code> \u4e2d\u662f\u5426\u5b58\u5728\u56db\u4e2a\u5143\u7d20 *a\uff0c**b\uff0cc* \u548c *d* \uff0c\u4f7f\u5f97 *a* + *b* + *c* + *d* \u7684\u503c\u4e0e <code>target<\/code> \u76f8\u7b49\uff1f\u627e\u51fa\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u4e14\u4e0d\u91cd\u590d\u7684\u56db\u5143\u7ec4\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>\u7b54\u6848\u4e2d\u4e0d\u53ef\u4ee5\u5305\u542b\u91cd\u590d\u7684\u56db\u5143\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"\">\u7ed9\u5b9a\u6570\u7ec4 nums = [1, 0, -1, 0, -2, 2]\uff0c\u548c target = 0\u3002\n\n\u6ee1\u8db3\u8981\u6c42\u7684\u56db\u5143\u7ec4\u96c6\u5408\u4e3a\uff1a\n[\n  [-1,  0, 0, 1],\n  [-2, -1, 1, 2],\n  [-2,  0, 0, 2]\n]\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<p><strong>\u601d\u8def\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u4e24\u4e2a\u5faa\u73af\u56fa\u5b9a\u4e24\u4e2a\u6570\uff0c\u7528\u53cc\u6307\u9488\u627e\u5230\u6ee1\u8db3\u9898\u89e3\u7684\u53e6\u5916\u4e24\u4e2a\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u53cc\u6307\u9488\u6cd5\uff0c\u89e3\u6cd5\u548c\u4e09\u6570\u4e4b\u548c\u7c7b\u4f3c<\/li>\n<li>\u8fd0\u7528\u526a\u679d\u76f4\u63a5\u6392\u9664\u91cd\u590d\u89e3<\/li>\n<li>\u5408\u7406\u79fb\u52a8\u53cc\u6307\u9488\u76f4\u63a5\u6392\u9664\u975e\u6b63\u786e\u89e3<\/li>\n<\/ul>\n<p><strong>\u91cd\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u600e\u6837\u526a\u9664\u6240\u6709\u91cd\u590d\u679d\uff0c\u5efa\u8bae\u4f7f\u7528list\u800c\u975eset\u5b58\u50a8\u7ed3\u679c\uff0c\u53bbdebug\u5206\u6790\u6709\u54ea\u4e9b\u91cd\u590d\u679d\u662f\u6ca1\u5904\u7406\u597d\u7684.<\/li>\n<li>\u600e\u6837\u901a\u8fc7\u7279\u5b9a\u6761\u4ef6\uff08\u4e00\u822c\u662f\u8fb9\u754c\u503c\uff09\u6392\u9664\u975e\u6b63\u786e\u89e3\u96c6\uff0c\u51cf\u5c11\u8fd0\u7b97\u6b21\u6570<\/li>\n<\/ul>\n<pre><code class=\"language-python \">from typing import List\n\n\nclass Solution(object):\n    def fourSum(self, nums: List[int], target: int) -&gt; List[List[int]]:\n\n        length = len(nums)\n        # result = set()\n        result = []\n\n        # \u53cc\u6307\u9488\u6cd5\u4f7f\u7528\u524d\u63d0\uff1a\u6392\u5e8f\n        nums.sort()\n\n        for i in range(length - 3):\n            # \u53bb\u91cd\uff08\u526a\u679d\uff09\n            if i &gt; 0 and nums[i] == nums[i - 1]:\n                continue\n            # \u5982\u679c\u56fa\u5b9a\u6570\u4e0e\u6570\u7ec4\u4e09\u6700\u5c0f\u6570\u4e4b\u548c\u5927\u4e8etarget, \u5219\u540e\u7eed\u5faa\u73af\u90fd\u662f\u4e0d\u5b58\u5728\u89e3\u7684, \u4ece\u904d\u5386\u4e2d\u8df3\u51fa\n            if nums[i] + sum(nums[i + 1:i + 3 + 1]) &gt; target:\n                break\n            # \u5982\u679c\u56fa\u5b9a\u6570\u4e0e\u6570\u7ec4\u4e09\u6700\u5927\u6570\u4e4b\u548c\u5c0f\u4e8etaget, \u5219\u5f53\u524d\u904d\u5386\u4e0d\u5b58\u5728\u89e3, \u8fdb\u5165\u4e0b\u4e00\u4e2a\u904d\u5386\n            if nums[i] + sum(nums[-1:-3 - 1:-1]) &lt; target:\n                continue\n\n            for j in range(i + 1, length - 2):\n                # \u53bb\u91cd\uff08\u526a\u679d\uff09\n                if j - i &gt; 1 and nums[j] == nums[j - 1]:\n                    continue\n                # \u5982\u679c\u56fa\u5b9a\u6570\u4e0e\u6570\u7ec4\u4e24\u6700\u5c0f\u6570\u4e4b\u548c\u5927\u4e8etarget, \u5219\u540e\u7eed\u5faa\u73af\u90fd\u662f\u4e0d\u5b58\u5728\u89e3\u7684, \u4ece\u904d\u5386\u4e2d\u8df3\u51fa\n                if nums[i] + nums[j] + sum(nums[j + 1:j + 2 + 1]) &gt; target:\n                    break\n                # \u5982\u679c\u56fa\u5b9a\u6570\u4e0e\u6570\u7ec4\u4e24\u6700\u5927\u6570\u4e4b\u548c\u5c0f\u4e8etarget, \u5219\u5f53\u524d\u904d\u5386\u4e0d\u5b58\u5728\u89e3, \u8fdb\u5165\u4e0b\u4e00\u4e2a\u904d\u5386\n                if nums[i] + nums[j] + sum(nums[-1:-2 - 1:-1]) &lt; target:\n                    continue\n\n                # \u53cc\u6307\u9488\u6cd5\n                left, right = j + 1, length - 1\n                while left &lt; right:\n                    tmp_sum = nums[i] + nums[j] + nums[left] + nums[right]\n                    # \u5982\u679c\u5f53\u524d\u548c\u5c0f\u4e8etarget, \u6536\u7f29\u5de6\u8fb9\u754c\n                    if tmp_sum &lt; target:\n                        left += 1\n                    # \u5982\u679c\u5f53\u524d\u548c\u5927\u4e8etarget, \u6536\u7f29\u5de6\u8fb9\u754c\n                    elif tmp_sum &gt; target:\n                        right -= 1\n                    # \u5982\u679c\u503c\u76f8\u7b49\n                    else:\n                        # \u8bb0\u5f55\u89e3\n                        # result.add((nums[i], nums[j], nums[left], nums[right], ))\n                        result.append([nums[i], nums[j], nums[left], nums[right]])\n\n                        # \u6c42\u5f97\u6b63\u786e\u89e3\u540e\uff0c\u53bb\u91cd\uff08\u526a\u679d\uff09\n                        while left &lt; right and nums[left] == nums[left + 1]:\n                            left += 1\n                        # \u6c42\u5f97\u6b63\u786e\u89e3\u540e\uff0c\u53bb\u91cd\uff08\u526a\u679d\uff09\n                        while left &lt; right and nums[right] == nums[right - 1]:\n                            right -= 1\n\n                        # \u5728\u6c42\u5f97\u6b63\u786e\u89e3\uff0c\u5e76\u4e14\u526a\u679d\u540e\uff0c\u4ec5\u6536\u7f29\u79fb\u52a8\u4e00\u4e2a\u6307\u9488\uff0c\u90fd\u4e0d\u4f1a\u662f\u6b63\u786e\u89e3\uff1b\n                        # \u56e0\u6b64\u5e94\u6536\u7f29\u79fb\u52a8\u53cc\u6307\u9488\uff0c\u76f4\u63a5\u6392\u9664\u4e0d\u7b26\u5408\u89e3\u7684\u60c5\u51b5\uff0c\u51cf\u5c11\u8fd0\u7b97\u6b21\u6570\n                        left += 1\n                        right -= 1\n\n        return result\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u8f6c\u5316\u6210\u4e0a\u4e00\u9898\u4e09\u6570\u4e4b\u548c\u95ee\u9898\uff0c<code>a + b + c = -d + target<\/code>\uff0c\u76f8\u5e94\u7684\u4e5f\u9700\u8981\u66f4\u6539<a href=\"https:\/\/leetcode-cn.com\/explore\/featured\/card\/all-about-lockup-table\/238\/lookup-table-related-sum-questions\/994\/\">\u4e09\u6570\u4e4b\u548c<\/a>\u7684 <code>threeSum<\/code> \u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n\n    def twoSum(self, nums, target, l, r):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \"\"\"\n\n        n = len(nums[l : r+1])\n        if n &lt; 2:\n            return list()\n\n        start = nums[l - 1]\n        dict1 = dict()\n        ret = list()\n        for i in range(l, r+1):\n            other = target - nums[i]\n            if other in dict1:\n                ret.append([start, other, nums[i]])\n            dict1[nums[i]] = i\n\n        return ret\n\n    # nums[l...r] \u4e2d\u8fd4\u56de a+b+c=target\n    def threeSum(self, nums, target, l, r):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \"\"\"\n\n        n = len(nums[l : r+1])\n        if n &lt; 3:\n            return list()\n\n        start = nums[l-1]\n        ret = list()\n        for i in range(l, r+1):\n            if nums[i] &gt; 0 and target &lt;= 0:\n                break\n            if (i &gt;= l+1 and nums[i] == nums[i-1]):\n                continue\n            ll = self.twoSum(nums, target-nums[i], i+1, r)\n            if (ll == list()):\n                continue\n            for i in ll:\n                i.insert(0, start)\n                ret.append(i)\n        return ret\n\n    def fourSum(self, nums, target):\n        \"\"\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[List[int]]\n        \"\"\"\n        n = len(nums)\n        if n &lt; 4:\n            return []\n        nums.sort()\n        ret = set()\n        for i in range(n):\n            ll = self.threeSum(nums, target-nums[i], i+1, n-1)\n            if ll == list():\n                continue\n            for l in ll:\n                ret.add(tuple(l))\n        return list(ret)\n<\/code><\/pre>\n<h3>\u7075\u6d3b\u9009\u62e9\u952e\u503c<\/h3>\n<p>\u5de7\u7528\u6b63\u786e\u7684\u952e\u503c\uff0c\u6709\u65f6\u53ef\u4ee5\u8ba9\u60a8\u7684\u4ee3\u7801\u4e8b\u534a\u529f\u500d\u3002<\/p>\n<h4>\u56db\u6570\u76f8\u52a0 II<\/h4>\n<p>\u7ed9\u5b9a\u56db\u4e2a\u5305\u542b\u6574\u6570\u7684\u6570\u7ec4\u5217\u8868 A , B , C , D ,\u8ba1\u7b97\u6709\u591a\u5c11\u4e2a\u5143\u7ec4 <code>(i, j, k, l)<\/code> \uff0c\u4f7f\u5f97 <code>A[i] + B[j] + C[k] + D[l] = 0<\/code>\u3002<\/p>\n<p>\u4e3a\u4e86\u4f7f\u95ee\u9898\u7b80\u5355\u5316\uff0c\u6240\u6709\u7684 A, B, C, D \u5177\u6709\u76f8\u540c\u7684\u957f\u5ea6 N\uff0c\u4e14 0 \u2264 N \u2264 500 \u3002\u6240\u6709\u6574\u6570\u7684\u8303\u56f4\u5728 -228 \u5230 228 &#8211; 1 \u4e4b\u95f4\uff0c\u6700\u7ec8\u7ed3\u679c\u4e0d\u4f1a\u8d85\u8fc7 231 &#8211; 1 \u3002<\/p>\n<p><strong>\u4f8b\u5982:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165:\nA = [ 1, 2]\nB = [-2,-1]\nC = [-1, 2]\nD = [ 0, 2]\n\n\u8f93\u51fa:\n2\n\n\u89e3\u91ca:\n\u4e24\u4e2a\u5143\u7ec4\u5982\u4e0b:\n1. (0, 0, 0, 1) -&gt; A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0\n2. (1, 1, 0, 0) -&gt; A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<pre><code class=\"language-python \">class Solution:\n    def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -&gt; int:\n        dic = collections.Counter(a+b for a in A for b in B)\n        return sum(dic.get(-c-d,0) for c in C for d in D)\n<\/code><\/pre>\n<ul>\n<li>\u8bb0\u5f55\u9700\u8981\u7684\u503c<\/li>\n<\/ul>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<ol>\n<li>\u66b4\u529b\u89e3\u6cd5\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^4)<\/li>\n<li>\u5c06 D \u4e2d\u7684\u5143\u7d20\u653e\u5165\u67e5\u627e\u8868\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^3)<\/li>\n<li>\u5c06 A + B \u7684\u6bcf\u4e00\u79cd\u53ef\u80fd\u653e\u5165\u67e5\u627e\u8868\uff0c\u7136\u540e \u4e24\u91cd\u5faa\u73af\u5bf9\u5728\u67e5\u627e\u8868\u4e2d\u5bfb\u627e\u662f\u5426\u5b58\u5728 -(C[i] + D[i])\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\u3002\u4e24\u6570\u76f8\u52a0\u7684\u548c\u4f5c\u4e3a\u952e\u503c\u3002<\/li>\n<\/ol>\n<p>\u6211\u91c7\u7528\u7b2c\u4e09\u79cd\u65b9\u6cd5\u5b9e\u73b0<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def fourSumCount(self, A, B, C, D):\n        \"\"\"\n        :type A: List[int]\n        :type B: List[int]\n        :type C: List[int]\n        :type D: List[int]\n        :rtype: int\n        \"\"\"\n        assert(len(A) == len(B) == len(C) == len(D))\n\n        memo = dict()\n        for i in range(len(A)):\n            for j in range(len(B)):\n                if A[i] + B[j] in memo:\n                    memo[A[i] + B[j]] += 1\n                else:\n                    memo[A[i] + B[j]] = 1\n\n        ret = 0\n        for i in range(len(C)):\n            for j in range(len(D)):\n                if -C[i] - D[j] in memo:\n                    ret += memo[-C[i]-D[j]]\n        return ret\n<\/code><\/pre>\n<h4>\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u5206\u7ec4<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5c06\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u6307\u5b57\u6bcd\u76f8\u540c\uff0c\u4f46\u6392\u5217\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: [\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"],\n\u8f93\u51fa:\n[\n  [\"ate\",\"eat\",\"tea\"],\n  [\"nat\",\"tan\"],\n  [\"bat\"]\n]\n\n<\/code><\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u6240\u6709\u8f93\u5165\u5747\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li>\u4e0d\u8003\u8651\u7b54\u6848\u8f93\u51fa\u7684\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<h5>\u89e3\u7b54<\/h5>\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u6392\u5e8f\u6570\u7ec4\u5206\u7c7b<\/strong><br \/>\n<strong>\u601d\u8def<\/strong><br \/>\n\u5f53\u4e14\u4ec5\u5f53\u5b83\u4eec\u7684\u6392\u5e8f\u5b57\u7b26\u4e32\u76f8\u7b49\u65f6\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5b57\u6bcd\u5f02\u4f4d\u8bcd\u3002<\/p>\n<p><strong>\u7b97\u6cd5<\/strong><br \/>\n\u7ef4\u62a4\u4e00\u4e2a\u6620\u5c04<code>ans : {String -&gt; List}<\/code>\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e K \u662f\u4e00\u4e2a\u6392\u5e8f\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u503c\u662f\u521d\u59cb\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u6392\u5e8f\u540e\u7b49\u4e8e K\u3002<br \/>\n\u5728 Java \u4e2d\uff0c\u6211\u4eec\u5c06\u952e\u5b58\u50a8\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff0c<code>code<\/code>\u3002 \u5728 Python \u4e2d\uff0c\u6211\u4eec\u5c06\u952e\u5b58\u50a8\u4e3a\u6563\u5217\u5316\u5143\u7ec4\uff0c\u4f8b\u5982\uff0c<code>('c', 'o', 'd', 'e')<\/code>\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/24\/90ac4e3fca704.png\" alt=\"49_groupanagrams1.png\" \/><\/p>\n<pre><code class=\"language-python \">class Solution(object):\n    def groupAnagrams(self, strs):\n        ans = collections.defaultdict(list)\n        for s in strs:\n            ans[tuple(sorted(s))].append(s)\n        return ans.values()\n<\/code><\/pre>\n<p><strong>\u590d\u6742\u5ea6\u5206\u6790<\/strong><\/p>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(NKlogK)\uff0c\u5176\u4e2d N \u662f <code>strs<\/code>\u7684\u957f\u5ea6\uff0c\u800c K \u662f <code>strs<\/code> \u4e2d\u5b57\u7b26\u4e32\u7684\u6700\u5927\u957f\u5ea6\u3002\u5f53\u6211\u4eec\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0c\u5916\u90e8\u5faa\u73af\u5177\u6709\u7684\u590d\u6742\u5ea6\u4e3aO(N)\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728 O(KlogK) \u7684\u65f6\u95f4\u5185\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u4e32\u6392\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(NK)\uff0c\u6392\u5e8f\u5b58\u50a8\u5728 ans \u4e2d\u7684\u5168\u90e8\u4fe1\u606f\u5185\u5bb9\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>\u5b98\u65b9\u89e3\u7b54\uff1a<\/h5>\n<p>\u5bf9\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u6bcf\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u6392\u5e8f\uff0c\u4e4b\u540e\u5982\u679c\u9047\u5230\u76f8\u7b49\u7684\u5b57\u7b26\u4e32\uff0c\u5c31\u8bf4\u660e\u8be5\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u6392\u5e8f\u524d\u7684\u5b57\u7b26\u4e32\u662f\u5b57\u6bcd\u6613\u4f4d\u8bcd\u3002\u627e\u5b57\u6bcd\u6613\u4f4d\u8bcd\u7684\u8fc7\u7a0b\u7528\u54c8\u5e0c\u8868\uff0c\u6bcf\u5f53\u51fa\u73b0\u4e00\u4e2a\u65b0\u7684\u5b57\u6bcd\u6613\u4f4d\u8bcd\uff0c\u5c31\u5f80 <code>ret<\/code> \u4e2d\u6dfb\u52a0\u4e00\u4e2a <code>list<\/code>\uff0c\u540c\u65f6\u5f80\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u8be5\u5b57\u6bcd\u6613\u4f4d\u8bcd\uff0c\u5e76\u5c06\u8be5\u5b57\u6bcd\u6613\u4f4d\u8bcd\u5728\u54c8\u5e0c\u8868\u4e2d\u7684 <code>value<\/code> \u503c\u5b58\u6210\u8be5 <code>list<\/code> \u7684\u7d22\u5f15\u503c\uff1b\u51fa\u73b0\u54c8\u5e0c\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u5b57\u6bcd\u6613\u4f4d\u8bcd\uff0c\u76f4\u63a5\u5f80\u5bf9\u5e94\u7d22\u5f15\u7684 <code>list<\/code> \u4e2d\u6dfb\u52a0\u5373\u53ef\u3002\u6392\u5e8f\u540e\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\u503c\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def groupAnagrams(self, strs):\n        \"\"\"\n        :type strs: List[str]\n        :rtype: List[List[str]]\n        \"\"\"\n\n        if len(strs) == 0:\n            return []\n\n        ret = []\n        memo = dict()\n        k = 0\n        for one in strs:\n            lone = list(one)\n            lone.sort()\n            tone = \"\".join(lone)\n            if tone not in memo:\n                memo[tone] = k\n                k += 1\n                ret.append([])\n            ret[memo[tone]].append(one)\n        return ret\n\n<\/code><\/pre>\n<h4>\u56de\u65cb\u9556\u7684\u6570\u91cf<\/h4>\n<p>\u7ed9\u5b9a\u5e73\u9762\u4e0a <em>n<\/em> \u5bf9\u4e0d\u540c\u7684\u70b9\uff0c\u201c\u56de\u65cb\u9556\u201d \u662f\u7531\u70b9\u8868\u793a\u7684\u5143\u7ec4 <code>(i, j, k)<\/code> \uff0c\u5176\u4e2d <code>i<\/code> \u548c <code>j<\/code> \u4e4b\u95f4\u7684\u8ddd\u79bb\u548c <code>i<\/code> \u548c <code>k<\/code> \u4e4b\u95f4\u7684\u8ddd\u79bb\u76f8\u7b49\uff08<strong>\u9700\u8981\u8003\u8651\u5143\u7ec4\u7684\u987a\u5e8f<\/strong>\uff09\u3002<\/p>\n<p>\u627e\u5230\u6240\u6709\u56de\u65cb\u9556\u7684\u6570\u91cf\u3002\u4f60\u53ef\u4ee5\u5047\u8bbe <em>n<\/em> \u6700\u5927\u4e3a <strong>500<\/strong>\uff0c\u6240\u6709\u70b9\u7684\u5750\u6807\u5728\u95ed\u533a\u95f4 <strong>[-10000, 10000]<\/strong> \u4e2d\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre><code class=\"language-python \">\u8f93\u5165:\n[[0,0],[1,0],[2,0]]\n\n\u8f93\u51fa:\n2\n\n\u89e3\u91ca:\n\u4e24\u4e2a\u56de\u65cb\u9556\u4e3a [[1,0],[0,0],[2,0]] \u548c [[1,0],[2,0],[0,0]]\n\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<ol>\n<li>\u5916\u5c42\u904d\u5386\u6bcf\u4e2a\u70b9 i\uff0c\u5185\u5c42\u904d\u5386\u5e76\u8ba1\u7b97\u5176\u4ed6\u70b9 j \u5230 i \u7684\u8ddd\u79bb\u5e76\u901a\u8fc7 Map \u4fdd\u5b58\u76f8\u7b49\u8ddd\u79bb\u7684\u9891\u6b21\u3002<\/li>\n<li>\u8ba1\u7b97\u8ddd\u79bb\u516c\u5f0f\u4e0d\u7528\u5f00\u6839\u53f7<\/li>\n<li>\u8ba1\u7b97\u6392\u5217\u7ec4\u5408\u516c\u5f0f n * (n &#8211; 1)<\/li>\n<\/ol>\n<pre><code class=\"language-python \">class Solution:\n    def dis(self, p1, p2):\n        return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])\n\n    def numberOfBoomerangs(self, points: List[List[int]]) -&gt; int:\n        res = 0\n        for i in points:\n            freqMap = {}\n            for j in points:\n                if j != i:\n                    d = self.dis(i, j)\n                    freqMap[d] = freqMap[d] + 1 if d in freqMap else 1\n\n            for v in freqMap.values():\n                res += v * (v - 1)\n\n        return res\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p><strong>\u66b4\u529b\u89e3\u6cd5<\/strong><br \/>\n\u4e09\u91cd\u5faa\u73af\u679a\u4e3e\u6240\u6709\u7684\u53ef\u80fd\u6027\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1aO(n^3)<br \/>\n<strong>\u4f7f\u7528\u67e5\u627e\u8868<\/strong><br \/>\n\u89c2\u5bdf\u5230 i \u662f\u4e00\u4e2a \u201c\u67a2\u7ebd\u201d\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u70b9i\uff0c\u904d\u5386\u5176\u4f59\u70b9\u5230 i \u7684\u8ddd\u79bb\u5bf9\u4e8e\u6bcf\u4e2a\u67a2\u7ebd i\uff0c\u8ba1\u7b97\u5b83\u5230\u5176\u5b83\u70b9j\u7684\u8ddd\u79bb\uff0c\u5e76\u5c06\u8ddd\u79bb\u4f5c\u4e3a\u952e\u5b58\u5165\u5b57\u5178 dict \u4e2d\uff0cvalue \u4e3a\u8ddd\u79bb\u7684\u4e2a\u6570\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1aO(n^2)\u3002 \u8ddd\u79bb\u4f5c\u4e3a\u952e\u503c\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def numberOfBoomerangs(self, points):\n        \"\"\"\n        :type points: List[List[int]]\n        :rtype: int\n        \"\"\"\n\n        n = len(points)\n        if n &lt; 3:\n            return 0\n\n\n        ret = 0\n        # i\u662f\u4e00\u4e2a\u4e2d\u8f74, \u8ba1\u7b97\u51fa\u6240\u6709\u4e0ei\u7684\u8ddd\u79bb\n        for i in range(n):\n            memo = dict()\n            for j in range(n):\n                if (i != j):\n                    dict1 = self.dist(points[i], points[j])\n                    if dict1 in memo:\n                        memo[dict1] += 1\n                    else:\n                        memo[dict1] = 1\n            for key,value in memo.items():\n                ret += value * (value-1)\n        return ret\n\n    def dist(self, alist, blist):\n        return (alist[0] - blist[0]) * (alist[0] - blist[0]) + (alist[1] - blist[1]) * (alist[1] - blist[1])\n<\/code><\/pre>\n<h4>\u76f4\u7ebf\u4e0a\u6700\u591a\u7684\u70b9\u6570<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u5e73\u9762\uff0c\u5e73\u9762\u4e0a\u6709 <em>n<\/em> \u4e2a\u70b9\uff0c\u6c42\u6700\u591a\u6709\u591a\u5c11\u4e2a\u70b9\u5728\u540c\u4e00\u6761\u76f4\u7ebf\u4e0a\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: [[1,1],[2,2],[3,3]]\n\u8f93\u51fa: 3\n\u89e3\u91ca:\n^\n|\n|        o\n|     o\n|  o\n+-------------&gt;\n0  1  2  3  4\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]\n\u8f93\u51fa: 4\n\u89e3\u91ca:\n^\n|\n|  o\n|     o        o\n|        o\n|  o        o\n+-------------------&gt;\n0  1  2  3  4  5  6\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<p><strong>\u65b9\u6cd5 1\uff1a\u679a\u4e3e<\/strong><\/p>\n<p>\u9996\u5148\u7b80\u5316\u8fd9\u4e2a\u95ee\u9898\uff1a\u627e\u51fa\u4e00\u6761\u901a\u8fc7\u70b9 <code>i<\/code> \u7684\u76f4\u7ebf\uff0c\u4f7f\u5f97\u7ecf\u8fc7\u6700\u591a\u7684\u70b9\u3002<\/p>\n<p>\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u5176\u5b9e\u53ea\u9700\u8981\u8003\u8651\u5f53\u524d\u70b9\u4e4b\u540e\u51fa\u73b0\u7684\u70b9 <code>i + 1 .. N - 1<\/code> \u5373\u53ef\uff0c\u56e0\u4e3a\u901a\u8fc7\u70b9 <code>i-2<\/code> \u7684\u76f4\u7ebf\u5df2\u7ecf\u5728\u641c\u7d22\u70b9 <code>i-2<\/code>\u7684\u8fc7\u7a0b\u4e2d\u8003\u8651\u8fc7\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/25\/10c99cdf0dfa3.png\" alt=\"3881bd909daf9b0877c0b275700beaf8d0d3d35d6f75d3d3d96bc802b12ac06b-149-1.png\" \/><\/p>\n<p>\u601d\u8def\u975e\u5e38\u7b80\u5355\uff1a\u753b\u4e00\u6761\u901a\u8fc7\u70b9 <code>i<\/code> \u548c\u4e4b\u540e\u51fa\u73b0\u7684\u70b9\u7684\u76f4\u7ebf\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u5b58\u50a8\u8fd9\u6761\u8fb9\u5e76\u8ba1\u6570\u4e3a <code>2<\/code> = \u5f53\u524d\u8fd9\u6761\u76f4\u7ebf\u4e0a\u6709\u4e24\u4e2a\u70b9\u3002<\/p>\n<p>\u5047\u8bbe\u73b0\u5728 <code>i &lt; i + k &lt; i + l<\/code> \u8fd9\u4e09\u4e2a\u70b9\u5728\u540c\u4e00\u6761\u76f4\u7ebf\u4e0a\uff0c\u5f53\u753b\u51fa\u4e00\u6761\u901a\u8fc7 <code>i<\/code> \u548c <code>i+l<\/code> \u7684\u76f4\u7ebf\u4f1a\u53d1\u73b0\u5df2\u7ecf\u8bb0\u5f55\u8fc7\u4e86\uff0c\u56e0\u6b64\u5bf9\u66f4\u65b0\u8fd9\u6761\u8fb9\u5bf9\u5e94\u7684\u8ba1\u6570\uff1a<code>count++<\/code>\u3002<\/p>\n<blockquote><p>\n  \u5982\u4f55\u5b58\u50a8\u4e00\u6761\u8fb9\uff1f\n<\/p><\/blockquote>\n<p>\u5982\u679c\u8fd9\u6761\u7ebf\u662f\u6c34\u5e73\u7684\uff0c\u4f8b\u5982 <code>y=c<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5e38\u6570 <code>c<\/code> \u4f5c\u4e3a\u6c34\u5e73\u76f4\u7ebf\u7684\u54c8\u5e0c\u8868\u7684\u952e\u3002<\/p>\n<p>\u6ce8\u610f\u5230\u8fd9\u91cc\u6240\u6709\u76f4\u7ebf\u90fd\u662f\u901a\u8fc7\u540c\u4e00\u4e2a\u70b9 <code>i<\/code> \u7684\uff0c\u56e0\u6b64\u5e76\u4e0d\u9700\u8981\u8bb0\u5f55 <code>c<\/code> \u7684\u503c\u800c\u53ea\u9700\u8981\u7edf\u8ba1\u6c34\u5e73\u76f4\u7ebf\u7684\u4e2a\u6570\u3002<\/p>\n<p>\u5269\u4e0b\u7684\u76f4\u7ebf\u53ef\u4ee5\u88ab\u8868\u793a\u6210 <code>x = slope * y + c<\/code>\uff0c\u540c\u6837 <code>c<\/code> \u4e5f\u662f\u4e0d\u9700\u8981\u7684\uff0c\u56e0\u4e3a\u6240\u6709\u76f4\u7ebf\u90fd\u901a\u8fc7\u540c\u4e00\u4e2a\u70b9 <code>i<\/code> \u6240\u4ee5\u53ea\u9700\u8981\u7528 <code>slope<\/code> \u4f5c\u4e3a\u76f4\u7ebf\u7684\u952e\u3002<\/p>\n<p>\u901a\u8fc7\u4e24\u4e2a\u70b9 <code>1<\/code> \u548c <code>2<\/code> \u7684\u76f4\u7ebf\u65b9\u7a0b\u53ef\u4ee5 <a href=\"https:\/\/baike.baidu.com\/item\/\u76f4\u7ebf\/4876?fr=aladdin\">\u7528\u5750\u6807\u8868\u793a<\/a>\u4e3a\uff1a<br \/>\n$$<br \/>\n\\frac{x-x_{1}}{x_{1}-x_{2}}=\\frac{y-y_{1}}{y_{1}-y_{2}}<br \/>\n$$<\/p>\n<p>\u8f6c\u6362\u6210 $x=s l o p e \\times y+c$ \u8868\u793a\u4e3a\uff1a<br \/>\n$$<br \/>\n\\text {slope}=\\frac{x_{1}-x_{2}}{y_{1}-y_{2}}<br \/>\n$$<br \/>\n\u6240\u4ee5\uff0c\u6700\u7ec8\u7684\u7b97\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316\u6700\u5927\u70b9\u6570 <code>max_count = 1<\/code> \u3002<\/li>\n<li>\u8fed\u4ee3\u6240\u6709\u7684\u70b9 <code>i<\/code> \u4ece <code>0<\/code> \u5230 <code>N - 2<\/code>\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u70b9 <code>i<\/code> \u627e\u51fa\u901a\u8fc7\u8be5\u70b9\u76f4\u7ebf\u7684\u6700\u5927\u70b9\u6570 <code>max_count_i<\/code>\uff1a\n<ul>\n<li>\u521d\u59cb\u5316\u901a\u8fc7\u70b9 <code>i<\/code> \u76f4\u7ebf\u7684\u6700\u5927\u70b9\u6570\uff1a<code>count = 1<\/code>\u3002<\/li>\n<li>\u8fed\u4ee3\u4e0b\u4e00\u4e2a\u9876\u70b9 <code>j<\/code> \u4ece <code>i+1<\/code> \u5230 <code>N-1<\/code>\u3002<\/li>\n<li>\u5982\u679c j \u548c i \u91cd\u5408\uff0c\u66f4\u65b0\u70b9 i \u76f8\u540c\u70b9\u7684\u4e2a\u6570\u3002<\/li>\n<li>\u5426\u5219\uff1a\n<ul>\n<li>\u4fdd\u5b58\u901a\u8fc7 i \u548c j \u7684\u76f4\u7ebf\u3002<\/li>\n<li>\u6bcf\u6b65\u66f4\u65b0 <code>count<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u7ed3\u679c max_count_i = count + duplicates\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u66f4\u65b0\u7ed3\u679c max_count = max(max_count, max_count_i)\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/25\/e8e6f193f7d82.png\" alt=\"1.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/25\/1529432153248.png\" alt=\"2.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/25\/de40081413e12.png\" alt=\"3.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/25\/f0f34d4afa5c6.png\" alt=\"4.png\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kanghaov-img-1256185664.file.myqcloud.com\/2019\/08\/25\/b8415dfa25598.png\" alt=\"5.png\" \/><\/p>\n<pre><code class=\"language-python \">class Solution:\n    def maxPoints(self, points):\n        \"\"\"\n        :type points: List[Point]\n        :rtype: int\n        \"\"\"    \n        def max_points_on_a_line_containing_point_i(i):\n            \"\"\"\n            Compute the max number of points\n            for a line containing point i.\n            \"\"\"\n            def add_line(i, j, count, duplicates):\n                \"\"\"\n                Add a line passing through i and j points.\n                Update max number of points on a line containing point i.\n                Update a number of duplicates of i point.\n                \"\"\"\n                # rewrite points as coordinates\n                x1 = points[i].x\n                y1 = points[i].y\n                x2 = points[j].x\n                y2 = points[j].y\n                # add a duplicate point\n                if x1 == x2 and y1 == y2:\n                    duplicates += 1\n                # add a horisontal line : y = const\n                elif y1 == y2:\n                    nonlocal horisontal_lines\n                    horisontal_lines += 1\n                    count = max(horisontal_lines, count)\n                # add a line : x = slope * y + c\n                # only slope is needed for a hash-map\n                # since we always start from the same point\n                else:\n                    slope = (x1 - x2) \/ (y1 - y2)\n                    lines[slope] = lines.get(slope, 1) + 1\n                    count = max(lines[slope], count)\n                return count, duplicates\n\n            # init lines passing through point i\n            lines, horisontal_lines = {}, 1\n            # One starts with just one point on a line : point i.\n            count = 1\n            # There is no duplicates of a point i so far.\n            duplicates = 0\n            # Compute lines passing through point i (fixed)\n            # and point j (interation).\n            # Update in a loop the number of points on a line\n            # and the number of duplicates of point i.\n            for j in range(i + 1, n):\n                count, duplicates = add_line(i, j, count, duplicates)\n            return count + duplicates\n\n        # If the number of points is less than 3\n        # they are all on the same line.\n        n = len(points)\n        if n &lt; 3:\n            return n\n\n        max_count = 1\n        # Compute in a loop a max number of points\n        # on a line containing point i.\n        for i in range(n - 1):\n            max_count = max(max_points_on_a_line_containing_point_i(i), max_count)\n        return max_count\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u4e24\u70b9\u53ef\u4ee5\u786e\u5b9a\u4e00\u6761\u76f4\u7ebf\uff0c\u90a3\u4e48\u9009\u62e9\u56fa\u5b9a\u4e00\u4e2a\u70b9\uff0c\u6c42\u5176\u4ed6\u70b9\u4e0e\u56fa\u5b9a\u70b9\u7684\u659c\u7387\uff0c\u5982\u679c\u659c\u7387\u76f8\u540c\uff0c\u90a3\u4e48\u659c\u7387\u76f8\u540c\u7684\u70b9\u5728\u540c\u4e00\u6761\u76f4\u7ebf\u4e0a\u3002\u540c\u65f6\u8981\u8003\u8651\uff0c\u659c\u7387\u53ef\u80fd\u4e3a\u65e0\u7a77\u5927\uff0c\u4e5f\u6709\u53ef\u80fd\u4e24\u4e2a\u70b9\u4e3a\u540c\u4e00\u4e2a\u70b9\u3002\u952e\u503c\u5e94\u8be5\u4e3a\uff1a\u659c\u7387\u3002<\/p>\n<pre><code class=\"language-python \"># Definition for a point.\n# class Point:\n#     def __init__(self, a=0, b=0):\n#         self.x = a\n#         self.y = b\n\nfrom decimal import *\nclass Solution:\n    def maxPoints(self, points):\n        \"\"\"\n        :type points: List[Point]\n        :rtype: int\n        \"\"\"\n\n        n = len(points)\n        if (n &lt;= 2):\n            return n\n\n        ret = 0\n        # \u5bf9\u4e8e\u6bcf\u4e00\u4e2ai,\u8ba1\u7b97\u5b83\u4e0e\u5176\u4ed6\u70b9\u7684\u659c\u7387\n        for i in range(n):\n            memo = {'inf': 0}\n            same = 0\n            for j in range(n):\n                if i != j:\n                    if points[i].x == points[j].x and points[i].y != points[j].y:\n                        memo['inf'] += 1\n                    elif (points[i].x != points[j].x):\n                        slope1 = self.slope(points[i], points[j])\n                        if slope1 in memo:\n                            memo[slope1] += 1\n                        else:\n                            memo[slope1] = 1\n                    else:\n                        same += 1\n            ret = max(ret, max(memo.values())+same)\n\n        return ret + 1\n    def slope(self, point1, point2):\n        return Decimal(point2.y - point1.y) \/ Decimal(point2.x - point1.x)\n<\/code><\/pre>\n<h5>\u53e6:<\/h5>\n<p>\u4e00\u53e5\u8bdd\u89e3\u91ca: \u56fa\u5b9a\u4e00\u70b9, \u627e\u5176\u4ed6\u70b9\u548c\u8fd9\u4e2a\u70b9\u7ec4\u6210\u76f4\u7ebf, \u7edf\u8ba1\u4ed6\u4eec\u7684\u659c\u7387!<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u91cd\u70b9: \u6c42\u659c\u7387.\u7528\u4e24\u79cd\u65b9\u6cd5<\/p>\n<ol>\n<li>\u7528\u6700\u5927\u7ea6\u6570\u65b9\u6cd5(gcd), \u628a\u4ed6\u5316\u6210\u6700\u7b80\u5f62\u5f0f, 3\/6 == 2\/4 == 1\/2<\/li>\n<li>\u9664\u6570(\u4e0d\u592a\u7cbe\u786e, \u901f\u5ea6\u5feb!)<\/li>\n<\/ol>\n<pre><code class=\"language-python \">class Solution:\n    def maxPoints(self, points: List[List[int]]) -&gt; int:\n        from collections import Counter, defaultdict\n        # \u6240\u6709\u70b9\u7edf\u8ba1\n        points_dict = Counter(tuple(point) for point in points)\n        # \u628a\u552f\u4e00\u70b9\u5217\u4e3e\u51fa\u6765\n        not_repeat_points = list(points_dict.keys())\n        n = len(not_repeat_points)\n        if n == 1: return points_dict[not_repeat_points[0]]\n        res = 0\n        # \u6c42\u6700\u5927\u516c\u7ea6\u6570\n        def gcd(x, y):\n            if y == 0:\n                return x\n            else:\n                return gcd(y, x % y)\n\n        for i in range(n - 1):\n            # \u70b91\n            x1, y1 = not_repeat_points[i][0], not_repeat_points[i][1]\n            # \u659c\u7387\n            slope = defaultdict(int)\n            for j in range(i + 1, n):\n                # \u70b92\n                x2, y2 = not_repeat_points[j][0], not_repeat_points[j][1]\n                dy, dx = y2 - y1, x2 - x1\n                # \u65b9\u5f0f\u4e00 \u5229\u7528\u516c\u7ea6\u6570\n                g = gcd(dy, dx)\n                if g != 0:\n                    dy \/\/= g\n                    dx \/\/= g\n                slope[\"{}\/{}\".format(dy, dx)] += points_dict[not_repeat_points[j]]\n                # --------------------\n                # \u65b9\u5f0f\u4e8c, \u5229\u7528\u9664\u6cd5(\u4e0d\u51c6\u786e, \u901f\u5ea6\u5feb)\n                # if dx == 0:\n                #     tmp = \"#\"\n                # else:\n                #     tmp = dy * 1000 \/ dx * 1000\n                # slope[tmp] += points_dict[not_repeat_points[j]]\n                #------------------------------\n            res = max(res, max(slope.values()) + points_dict[not_repeat_points[i]])\n        return res\n<\/code><\/pre>\n<h3>\u67e5\u627e\u8868\u548c\u6ed1\u52a8\u7a97\u53e3<\/h3>\n<p>\u7ed3\u5408\u4f7f\u7528\u6ed1\u52a8\u7a97\u53e3\u548c\u67e5\u627e\u8868\uff0c\u4e0d\u65ad\u67e5\u627e\u5f53\u524d\u6ed1\u52a8\u7a97\u53e3\u5185\u6709\u6ca1\u6709\u91cd\u590d\u7684\u503c\u3002<\/p>\n<h4>\u5b58\u5728\u91cd\u590d\u5143\u7d20 II<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u548c\u4e00\u4e2a\u6574\u6570 <em>k<\/em>\uff0c\u5224\u65ad\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u7d22\u5f15 <em>i<\/em> \u548c <em>j<\/em>\uff0c\u4f7f\u5f97 <strong>nums [i] = nums [j]<\/strong>\uff0c\u5e76\u4e14 <em>i<\/em> \u548c <em>j<\/em> \u7684\u5dee\u7684\u7edd\u5bf9\u503c\u6700\u5927\u4e3a <em>k<\/em>\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums = [1,2,3,1], k = 3\n\u8f93\u51fa: true\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums = [1,0,1,1], k = 1\n\u8f93\u51fa: true\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums = [1,2,3,1,2,3], k = 2\n\u8f93\u51fa: false\n\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<ul>\n<li>\u904d\u5386\u5217\u8868\uff0c\u6bcf\u6b21\u90fd\u6bd4\u5bf9\u6700\u5c0f\u95f4\u9694\uff0c\u5e76\u66f4\u65b0\u54c8\u5e0c\u8868\u7d22\u5f15\uff0c\u5f53\u524d\u4f4d\u7f6e\u5f80\u5de6\u7684\u6700\u5c0f\u95f4\u9694\u4e00\u5b9a\u662f\u4e0e\u4e0a\u4e00\u6b21\u540c\u6570\u5b57\u51fa\u73b0\u7684\u7d22\u5f15\u7684\u8ddd\u79bb<\/li>\n<\/ul>\n<pre><code class=\"language-python \">class Solution:\n    def containsNearbyDuplicate(self, nums: List[int], k: int) -&gt; bool:\n        r, d = k + 1, {}\n        for i, n in enumerate(nums):\n            r, d[n] = min(r, i - d.get(n, -k - 1)), i\n        return r &lt;= k\n\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u7ed3\u5408\u4f7f\u7528\u6ed1\u52a8\u7a97\u53e3\u548c\u67e5\u627e\u8868\uff0c\u4e0d\u65ad\u67e5\u627e\u5f53\u524d\u6ed1\u52a8\u7a97\u53e3\u5185\u6709\u6ca1\u6709\u91cd\u590d\u503c\u3002\u6211\u4eec\u901a\u8fc7\u5efa\u7acb\u4e00\u4e2a <code>record<\/code> \u67e5\u627e\u8868\uff0c\u8868\u4e2d\u5b58\u7684\u662f\u7a97\u53e3\u4e2d\u7684\u6570\uff0c\u53e6\u5916\u6211\u4eec\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u7a97\u53e3\u7684\u5927\u5c0f > k \u7684\u65f6\u5019\uff0c\u6211\u4eec\u8981\u79fb\u9664 <code>record<\/code> \u4e2d\u6700\u5de6\u8fb9\u7684\u5143\u7d20\uff08\u4fdd\u8bc1\u6211\u4eec\u7a97\u53e3\u4e2d\u6709 &lt;= k \u4e2a\u6570\uff09\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsNearbyDuplicate(self, nums, k):\n        \"\"\"\n        :type nums: List[int]\n        :type k: int\n        :rtype: bool\n        \"\"\"\n\n        n = len(nums)\n\n        if (n &lt;= 1):\n            return False\n\n        recode = set()\n\n        for i in range(n):\n            if nums[i] in recode:\n                return True\n            recode.add(nums[i])\n            if len(recode) &gt; k:\n                recode.remove(nums[i - k])\n\n        return False\n\n<\/code><\/pre>\n<h4>\u5b58\u5728\u91cd\u590d\u5143\u7d20 III<\/h4>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5224\u65ad\u6570\u7ec4\u4e2d\u662f\u5426\u6709\u4e24\u4e2a\u4e0d\u540c\u7684\u7d22\u5f15 <em>i<\/em> \u548c <em>j<\/em>\uff0c\u4f7f\u5f97 <strong>nums [i]<\/strong> \u548c <strong>nums [j]<\/strong> \u7684\u5dee\u7684\u7edd\u5bf9\u503c\u6700\u5927\u4e3a <em>t<\/em>\uff0c\u5e76\u4e14 <em>i<\/em> \u548c <em>j<\/em> \u4e4b\u95f4\u7684\u5dee\u7684\u7edd\u5bf9\u503c\u6700\u5927\u4e3a <em>\u0137<\/em>\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums = [1,2,3,1], k = 3, t = 0\n\u8f93\u51fa: true\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums = [1,0,1,1], k = 1, t = 2\n\u8f93\u51fa: true\n\n<\/code><\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><code class=\"\">\u8f93\u5165: nums = [1,5,9,1,5,9], k = 2, t = 3\n\u8f93\u51fa: false\n\n<\/code><\/pre>\n<h5>\u89e3\u7b54<\/h5>\n<p>\u601d\u60f3\u7c7b\u4f3c\u6876\u6392\u5e8f\u7b97\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u5bbd\u5ea6\u4e3a(t+1)\u7684\u8fde\u7eed\u7684\u6876\u53ef\u4ee5\u8986\u76d6\u6389\u6240\u6709\u7684\u6570\u5b57\u8303\u56f4\uff0c\u90a3\u4e48\u5bf9\u4e8e\u5dee\u7684\u7edd\u5bf9\u503c\u6700\u5927\u4e3at\u7684\u4e24\u4e2a\u6570\uff0c\u6709\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\uff081\uff09\u8fd9\u4e24\u4e2a\u6570\u5b57\u5728\u540c\u4e00\u4e2a\u6876\u4e2d<\/li>\n<li>\uff082\uff09\u8fd9\u4e24\u4e2a\u6570\u5b57\u5728\u76f8\u90bb\u6876\u4e2d<\/li>\n<\/ul>\n<pre><code class=\"language-python \">def containsNearbyAlmostDuplicate(self, nums, k, t):\n    if t &lt; 0: return False\n    n = len(nums)\n    d = {}\n    w = t + 1\n    for i in range(n):\n        m = nums[i] \/\/ w\n        if m in d:\n            return True\n        if m - 1 in d and abs(nums[i] - d[m - 1]) &lt; w:\n            return True\n        if m + 1 in d and abs(nums[i] - d[m + 1]) &lt; w:\n            return True\n        d[m] = nums[i]\n        if i &gt;= k: del d[nums[i - k] \/\/ w]\n    return False\n<\/code><\/pre>\n<h5>\u5b98\u65b9\u89e3\u7b54<\/h5>\n<p>\u4e5f\u662f\u67e5\u627e\u8868\u4e0e\u6ed1\u52a8\u7a97\u53e3\u7684\u601d\u8def\uff1a\u7ef4\u6301\u6ed1\u52a8\u7a97\u7684\u5927\u5c0f\u6700\u5927\u4e3a k\uff0c\u904d\u5386\u6bcf\u4e00\u4e2a\u5143\u7d20 nums[i]\uff0c\u5728\u6d3b\u52a8\u7a97\u53e3\u4e2d\u5bfb\u627e |one-nums[i]| &lt; t\uff0c\u5373\u7a97\u53e3\u4e2d\u7684\u5143\u7d20\u8303\u56f4\u4e3a\uff1a[one-t \u2026 one+t] \u4e4b\u95f4\u3002<\/p>\n<pre><code class=\"language-python \">class Solution:\n    def containsNearbyAlmostDuplicate(self, nums, k, t):\n        \"\"\"\n        :type nums: List[int]\n        :type k: int\n        :type t: int\n        :rtype: bool\n        \"\"\"\n\n        n = len(nums)\n        if n &lt;= 1:\n            return False\n\n        recode = set()\n\n        for i in range(n):\n            if t == 0:\n                if nums[i] in recode:\n                    return True\n            else:\n                for one in recode:\n                    if abs(nums[i] - one) &lt;= t:\n                        return True\n            recode.add(nums[i])\n\n            if (len(recode) &gt; k):\n                recode.remove(nums[i - k])\n\n        return False\n<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u6211\u4eec\u77e5\u9053\u5728\u51c6\u5907\u9762\u8bd5\u7684\u65f6\u5019\uff0c\u5237\u7b97\u6cd5\u9898\u662f\u4e00\u79cd\u6377\u5f84\uff0c\u7279\u522b\u662f\u5237 LeetCode\uff0c\u4f46\u662f\u4e0d\u80fd\u4e00\u5473\u7684\u5237\u9898\uff0c\u6211\u4eec\u9700\u8981\u603b\u7ed3\u548c\u601d\u8003\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u76f8\u4f3c\u7684\u9898\u76ee\u6211\u4eec\u5e94\u8be5\u591a\u60f3\u60f3\u4ed6\u4eec\u7684\u601d\u60f3\uff0c\u5176\u5b9e\u5f88\u591a\u9898\u7684\u89e3\u9898\u601d\u8def\u90fd\u662f\u76f8\u8fd1\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u7cbe\u6790 \u4ecb\u7ecd \u67e5\u627e\uff0c\u662f\u4f7f\u7528\u8ba1\u7b97\u673a\u5904\u7406\u95ee\u9898\u65f6\u7684\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u4efb\u52a1\uff0c\u56e0\u6b64\u4e5f\u662f\u7b97\u6cd5\u9762\u8bd5\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u7c7b\u95ee\u9898\u3002\u5f88\u591a\u7b97\u6cd5\u95ee\u9898\u7684\u672c\u8d28\uff0c\u5c31\u662f\u8981\u80fd\u591f\u9ad8\u6548\u4f7f\u7528\u67e5\u627e\u3002LeetCode \u4e2d\u6709\u5f88\u591a\u95ee\u9898\u90fd\u4f1a\u7528\u5230\u96c6\u5408\u548c\u5b57\u5178\uff08C++ \u4e2d\u4e3a set \u548c map\uff0cPython \u4e2d\u4e3a set \u548c dict\uff09\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u4eca\u5929\u6211\u4eec\u5c06\u4f1a\u5bf9 LeetCode \u8fd9\u7c7b\u95ee\u9898\u8fdb\u884c\u603b\u7ed3\uff0c\u6240\u6709\u4ee3\u7801\u91c7\u7528 Python \u5b9e\u73b0\uff0c\u5e76\u53ef\u4ee5\u5728 LeetCode \u91cc AC\u3002 \u4e24\u7c7b\u67e5\u627e\u95ee\u9898 \u67e5\u627e\u6709\u65e0\uff1a\u5143\u7d20 &#8216;a&#8217; \u662f\u5426\u5b58\u5728\uff1f\u53ef\u4ee5\u4f7f\u7528 set \u96c6\u5408\u8fd9\u79cd\u6570\u636e\u7ed3\u6784 \u67e5\u627e\u5bf9\u5e94\u5173\u7cfb\uff08\u952e\u503c\u5bf9\u5e94\uff09\uff1a\u5143\u7d20 &#8216;a&#8217; \u51fa\u73b0\u4e86\u51e0\u6b21\uff1f\u53ef\u4ee5\u4f7f\u7528 dict \u5b57\u5178\u8fd9\u79cd\u6570\u636e\u7ed3\u6784 \u96c6\u5408 set \u7684\u4f7f\u7528 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4ece set \u5f00\u59cb\uff0c\u901a\u8fc7\u4e0b\u5217\u7684\u4e00\u4e9b\u95ee\u9898\u719f\u6089\u5b83\u7684\u4f7f\u7528\u5427\uff01 \u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6 \u7ed9\u5b9a\u4e24\u4e2a\u6570\u7ec4\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u4ea4\u96c6\u3002 \u793a\u4f8b 1: \u8f93\u5165: nums1 = [1,2,2,1], nums2 = [2,2] \u8f93\u51fa: [2] \u793a\u4f8b 2: \u8f93\u5165: nums1 = [4,9,5], [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":292,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[24,10,50],"class_list":["post-315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm","tag-leetcode","tag-python","tag-50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790 - Nemo<\/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:\/\/nemo.cool\/315.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790 - Nemo\" \/>\n<meta property=\"og:description\" content=\"\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u7cbe\u6790 \u4ecb\u7ecd \u67e5\u627e\uff0c\u662f\u4f7f\u7528\u8ba1\u7b97\u673a\u5904\u7406\u95ee\u9898\u65f6\u7684\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u4efb\u52a1\uff0c\u56e0\u6b64\u4e5f\u662f\u7b97\u6cd5\u9762\u8bd5\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u7c7b\u95ee\u9898\u3002\u5f88\u591a\u7b97\u6cd5\u95ee\u9898\u7684\u672c\u8d28\uff0c\u5c31\u662f\u8981\u80fd\u591f\u9ad8\u6548\u4f7f\u7528\u67e5\u627e\u3002LeetCode \u4e2d\u6709\u5f88\u591a\u95ee\u9898\u90fd\u4f1a\u7528\u5230\u96c6\u5408\u548c\u5b57\u5178\uff08C++ \u4e2d\u4e3a set \u548c map\uff0cPython \u4e2d\u4e3a set \u548c dict\uff09\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u4eca\u5929\u6211\u4eec\u5c06\u4f1a\u5bf9 LeetCode \u8fd9\u7c7b\u95ee\u9898\u8fdb\u884c\u603b\u7ed3\uff0c\u6240\u6709\u4ee3\u7801\u91c7\u7528 Python \u5b9e\u73b0\uff0c\u5e76\u53ef\u4ee5\u5728 LeetCode \u91cc AC\u3002 \u4e24\u7c7b\u67e5\u627e\u95ee\u9898 \u67e5\u627e\u6709\u65e0\uff1a\u5143\u7d20 &#8216;a&#8217; \u662f\u5426\u5b58\u5728\uff1f\u53ef\u4ee5\u4f7f\u7528 set \u96c6\u5408\u8fd9\u79cd\u6570\u636e\u7ed3\u6784 \u67e5\u627e\u5bf9\u5e94\u5173\u7cfb\uff08\u952e\u503c\u5bf9\u5e94\uff09\uff1a\u5143\u7d20 &#8216;a&#8217; \u51fa\u73b0\u4e86\u51e0\u6b21\uff1f\u53ef\u4ee5\u4f7f\u7528 dict \u5b57\u5178\u8fd9\u79cd\u6570\u636e\u7ed3\u6784 \u96c6\u5408 set \u7684\u4f7f\u7528 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4ece set \u5f00\u59cb\uff0c\u901a\u8fc7\u4e0b\u5217\u7684\u4e00\u4e9b\u95ee\u9898\u719f\u6089\u5b83\u7684\u4f7f\u7528\u5427\uff01 \u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6 \u7ed9\u5b9a\u4e24\u4e2a\u6570\u7ec4\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u4ea4\u96c6\u3002 \u793a\u4f8b 1: \u8f93\u5165: nums1 = [1,2,2,1], nums2 = [2,2] \u8f93\u51fa: [2] \u793a\u4f8b 2: \u8f93\u5165: nums1 = [4,9,5], [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nemo.cool\/315.html\" \/>\n<meta property=\"og:site_name\" content=\"Nemo\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-25T13:32:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-02T07:50:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/2019-01-09-19-22-01.png\" \/>\n\t<meta property=\"og:image:width\" content=\"3316\" \/>\n\t<meta property=\"og:image:height\" content=\"888\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nemo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nemo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html\"},\"author\":{\"name\":\"Nemo\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"headline\":\"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790\",\"datePublished\":\"2019-08-25T13:32:20+00:00\",\"dateModified\":\"2019-11-02T07:50:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html\"},\"wordCount\":243,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"image\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/2019-01-09-19-22-01.png\",\"keywords\":[\"leetcode\",\"python\",\"\u67e5\u627e\u8868\"],\"articleSection\":[\"Algorithm Notes\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nemo.cool\\\/315.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html\",\"url\":\"https:\\\/\\\/nemo.cool\\\/315.html\",\"name\":\"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790 - Nemo\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/2019-01-09-19-22-01.png\",\"datePublished\":\"2019-08-25T13:32:20+00:00\",\"dateModified\":\"2019-11-02T07:50:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nemo.cool\\\/315.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#primaryimage\",\"url\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/2019-01-09-19-22-01.png\",\"contentUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/2019-01-09-19-22-01.png\",\"width\":3316,\"height\":888},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/315.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/nemo.cool\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/#website\",\"url\":\"https:\\\/\\\/nemo.cool\\\/\",\"name\":\"Nemo\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nemo.cool\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\",\"name\":\"Nemo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"url\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"caption\":\"Nemo\"},\"logo\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\"},\"sameAs\":[\"https:\\\/\\\/nemo.cool\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790 - Nemo","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:\/\/nemo.cool\/315.html","og_locale":"en_US","og_type":"article","og_title":"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790 - Nemo","og_description":"\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u7cbe\u6790 \u4ecb\u7ecd \u67e5\u627e\uff0c\u662f\u4f7f\u7528\u8ba1\u7b97\u673a\u5904\u7406\u95ee\u9898\u65f6\u7684\u4e00\u4e2a\u6700\u57fa\u672c\u7684\u4efb\u52a1\uff0c\u56e0\u6b64\u4e5f\u662f\u7b97\u6cd5\u9762\u8bd5\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u7c7b\u95ee\u9898\u3002\u5f88\u591a\u7b97\u6cd5\u95ee\u9898\u7684\u672c\u8d28\uff0c\u5c31\u662f\u8981\u80fd\u591f\u9ad8\u6548\u4f7f\u7528\u67e5\u627e\u3002LeetCode \u4e2d\u6709\u5f88\u591a\u95ee\u9898\u90fd\u4f1a\u7528\u5230\u96c6\u5408\u548c\u5b57\u5178\uff08C++ \u4e2d\u4e3a set \u548c map\uff0cPython \u4e2d\u4e3a set \u548c dict\uff09\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u4eca\u5929\u6211\u4eec\u5c06\u4f1a\u5bf9 LeetCode \u8fd9\u7c7b\u95ee\u9898\u8fdb\u884c\u603b\u7ed3\uff0c\u6240\u6709\u4ee3\u7801\u91c7\u7528 Python \u5b9e\u73b0\uff0c\u5e76\u53ef\u4ee5\u5728 LeetCode \u91cc AC\u3002 \u4e24\u7c7b\u67e5\u627e\u95ee\u9898 \u67e5\u627e\u6709\u65e0\uff1a\u5143\u7d20 &#8216;a&#8217; \u662f\u5426\u5b58\u5728\uff1f\u53ef\u4ee5\u4f7f\u7528 set \u96c6\u5408\u8fd9\u79cd\u6570\u636e\u7ed3\u6784 \u67e5\u627e\u5bf9\u5e94\u5173\u7cfb\uff08\u952e\u503c\u5bf9\u5e94\uff09\uff1a\u5143\u7d20 &#8216;a&#8217; \u51fa\u73b0\u4e86\u51e0\u6b21\uff1f\u53ef\u4ee5\u4f7f\u7528 dict \u5b57\u5178\u8fd9\u79cd\u6570\u636e\u7ed3\u6784 \u96c6\u5408 set \u7684\u4f7f\u7528 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4ece set \u5f00\u59cb\uff0c\u901a\u8fc7\u4e0b\u5217\u7684\u4e00\u4e9b\u95ee\u9898\u719f\u6089\u5b83\u7684\u4f7f\u7528\u5427\uff01 \u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6 \u7ed9\u5b9a\u4e24\u4e2a\u6570\u7ec4\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u4ea4\u96c6\u3002 \u793a\u4f8b 1: \u8f93\u5165: nums1 = [1,2,2,1], nums2 = [2,2] \u8f93\u51fa: [2] \u793a\u4f8b 2: \u8f93\u5165: nums1 = [4,9,5], [&hellip;]","og_url":"https:\/\/nemo.cool\/315.html","og_site_name":"Nemo","article_published_time":"2019-08-25T13:32:20+00:00","article_modified_time":"2019-11-02T07:50:22+00:00","og_image":[{"width":3316,"height":888,"url":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/2019-01-09-19-22-01.png","type":"image\/png"}],"author":"Nemo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nemo","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nemo.cool\/315.html#article","isPartOf":{"@id":"https:\/\/nemo.cool\/315.html"},"author":{"name":"Nemo","@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"headline":"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790","datePublished":"2019-08-25T13:32:20+00:00","dateModified":"2019-11-02T07:50:22+00:00","mainEntityOfPage":{"@id":"https:\/\/nemo.cool\/315.html"},"wordCount":243,"commentCount":0,"publisher":{"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"image":{"@id":"https:\/\/nemo.cool\/315.html#primaryimage"},"thumbnailUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/2019-01-09-19-22-01.png","keywords":["leetcode","python","\u67e5\u627e\u8868"],"articleSection":["Algorithm Notes"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nemo.cool\/315.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nemo.cool\/315.html","url":"https:\/\/nemo.cool\/315.html","name":"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790 - Nemo","isPartOf":{"@id":"https:\/\/nemo.cool\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nemo.cool\/315.html#primaryimage"},"image":{"@id":"https:\/\/nemo.cool\/315.html#primaryimage"},"thumbnailUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/2019-01-09-19-22-01.png","datePublished":"2019-08-25T13:32:20+00:00","dateModified":"2019-11-02T07:50:22+00:00","breadcrumb":{"@id":"https:\/\/nemo.cool\/315.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nemo.cool\/315.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nemo.cool\/315.html#primaryimage","url":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/2019-01-09-19-22-01.png","contentUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2019\/08\/2019-01-09-19-22-01.png","width":3316,"height":888},{"@type":"BreadcrumbList","@id":"https:\/\/nemo.cool\/315.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/nemo.cool\/"},{"@type":"ListItem","position":2,"name":"LeetCode-\u67e5\u627e\u8868\u7c7b\u7b97\u6cd5\u9898\u7cbe\u6790"}]},{"@type":"WebSite","@id":"https:\/\/nemo.cool\/#website","url":"https:\/\/nemo.cool\/","name":"Nemo","description":"","publisher":{"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nemo.cool\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2","name":"Nemo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","url":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","contentUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","caption":"Nemo"},"logo":{"@id":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg"},"sameAs":["https:\/\/nemo.cool"]}]}},"_links":{"self":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":0,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/media\/292"}],"wp:attachment":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}