{"id":677,"date":"2020-09-26T11:09:59","date_gmt":"2020-09-26T03:09:59","guid":{"rendered":"http:\/\/39.96.58.60\/?p=677"},"modified":"2022-10-18T16:38:23","modified_gmt":"2022-10-18T08:38:23","slug":"leetcode-1356-%e6%a0%b9%e6%8d%ae%e6%95%b0%e5%ad%97%e4%ba%8c%e8%bf%9b%e5%88%b6%e4%b8%8b-1-%e7%9a%84%e6%95%b0%e7%9b%ae%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=677","title":{"rendered":"Leetcode 1356. \u6839\u636e\u6570\u5b57\u4e8c\u8fdb\u5236\u4e0b 1 \u7684\u6570\u76ee\u6392\u5e8f"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>arr<\/code>&nbsp;\u3002\u8bf7\u4f60\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6309\u7167\u5176\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\u6570\u5b57 <strong>1<\/strong> \u7684\u6570\u76ee\u5347\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>\u5982\u679c\u5b58\u5728\u591a\u4e2a\u6570\u5b57\u4e8c\u8fdb\u5236\u4e2d&nbsp;<strong>1<\/strong>&nbsp;\u7684\u6570\u76ee\u76f8\u540c\uff0c\u5219\u5fc5\u987b\u5c06\u5b83\u4eec\u6309\u7167\u6570\u503c\u5927\u5c0f\u5347\u5e8f\u6392\u5217\u3002<\/p>\n<p>\u8bf7\u4f60\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [0,1,2,3,4,5,6,7,8]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[0,1,2,4,8,3,5,6,7]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>[0] \u662f\u552f\u4e00\u4e00\u4e2a\u6709 0 \u4e2a 1 \u7684\u6570\u3002\n\n[1,2,4,8] \u90fd\u6709 1 \u4e2a 1 \u3002\n\n[3,5,6] \u6709 2 \u4e2a 1 \u3002\n\n[7] \u6709 3 \u4e2a 1 \u3002\n\n\u6309\u7167 1 \u7684\u4e2a\u6570\u6392\u5e8f\u5f97\u5230\u7684\u7ed3\u679c\u6570\u7ec4\u4e3a [0,1,2,4,8,3,5,6,7]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1024,512,256,128,64,32,16,8,4,2,1]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,4,8,16,32,64,128,256,512,1024]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6570\u7ec4\u4e2d\u6240\u6709\u6574\u6570\u4e8c\u8fdb\u5236\u4e0b\u90fd\u53ea\u6709 1 \u4e2a 1 \uff0c\u6240\u4ee5\u4f60\u9700\u8981\u6309\u7167\u6570\u503c\u5927\u5c0f\u5c06\u5b83\u4eec\u6392\u5e8f\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [10000,10000]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[10000,10000]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [2,3,5,7,11,13,17,19]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[2,3,5,17,7,11,13,19]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [10,100,1000,10000]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[10,100,10000,1000]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= arr.length &lt;= 500<\/code><\/li>\n<li><code>0 &lt;= arr[i] &lt;= 10^4<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6392\u5e8f\u3001 \u4f4d\u8fd0\u7b97\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a64 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8626.50% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8625.81% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def sortByBits(self, arr: List[int]) -&gt; List[int]:\n        record = dict() # {int:list} \u51fa\u73b0int\u6b211 \u7684\u5404\u6570\u7ec4\u6210\u7684\u5217\u8868\n        for i in range(len(arr)):\n            a = arr[i]\n            num = 0\n            while a &gt; 0:\n                num += (a == a | 1) # \u770b\u6700\u540e\u4f4d\u662f\u5426\u662f1\n                a = a &gt;&gt; 1  # \u53f3\u79fb\n            if num not in record:   # \u5b58\u5165\u5b57\u6bb5\n                record[num] = [arr[i]]\n            else:\n                record[num].append(arr[i])\n        result = []\n        keys_sort = list(record.keys()) # \u5bf9\u5b57\u5178key\u8fdb\u884c\u6392\u5e8f\n        keys_sort.sort()\n        for key in keys_sort:\n            record[key].sort()\n            result.extend(record[key])\n        return result\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a68 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8620.30% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8622.39% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5c06\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff0c\u7136\u540e\u7edf\u8ba1\u51fa\u73b0\u76841.\u7136\u540e\u6392\u5e8f\n\"\"\"\nclass Solution:\n    def sortByBits(self, arr: List[int]) -&gt; List[int]:\n        arr.sort(key=lambda x:(bin(x).count('1'), x))\n        return arr<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;arr&nbsp;\u3002\u8bf7\u4f60\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6309\u7167\u5176\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\u6570\u5b57 1 \u7684\u6570\u76ee\u5347\u5e8f\u6392\u5e8f\u3002&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/677"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=677"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/677\/revisions"}],"predecessor-version":[{"id":678,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/677\/revisions\/678"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}