{"id":6353,"date":"2020-02-22T22:42:47","date_gmt":"2020-02-23T06:42:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6353"},"modified":"2020-02-22T22:46:57","modified_gmt":"2020-02-23T06:46:57","slug":"leetcode-1356-sort-integers-by-the-number-of-1-bits","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1356-sort-integers-by-the-number-of-1-bits\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1356. Sort Integers by The Number of 1 Bits"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>arr<\/code>. You have to sort the integers in the array&nbsp;in ascending order by the number of&nbsp;<strong>1&#8217;s<\/strong>&nbsp;in their binary representation and in case of two or more integers have the same number of&nbsp;<strong>1&#8217;s<\/strong>&nbsp;you have to sort them in ascending order.<\/p>\n\n\n\n<p>Return&nbsp;<em>the sorted array<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [0,1,2,3,4,5,6,7,8]\n<strong>Output:<\/strong> [0,1,2,4,8,3,5,6,7]\n<strong>Explantion:<\/strong> [0] is the only integer with 0 bits.\n[1,2,4,8] all have 1 bit.\n[3,5,6] have 2 bits.\n[7] has 3 bits.\nThe sorted array by bits is [0,1,2,4,8,3,5,6,7]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1024,512,256,128,64,32,16,8,4,2,1]\n<strong>Output:<\/strong> [1,2,4,8,16,32,64,128,256,512,1024]\n<strong>Explantion:<\/strong> All integers have 1 bit in the binary representation, you should just sort them in ascending order.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [10000,10000]\n<strong>Output:<\/strong> [10000,10000]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [2,3,5,7,11,13,17,19]\n<strong>Output:<\/strong> [2,3,5,17,7,11,13,19]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [10,100,1000,10000]\n<strong>Output:<\/strong> [10,100,10000,1000]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 500<\/code><\/li><li><code>0 &lt;= arr[i] &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> sortByBits(vector<int>& arr) {\n    sort(begin(arr), end(arr), [](const int a, const int b){\n      int key_a = __builtin_popcount(a);\n      int key_b = __builtin_popcount(b);\n      if (key_a != key_b) return key_a < key_b;\n      return a < b;\n    });\n    return arr;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"Python\">\n# Author: Huahua\nclass Solution:\n  def sortByBits(self, arr: List[int]) -> List[int]:\n    return sorted(arr, key=lambda x: (bin(x).count('1') << 16) + x)<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;arr. You have to sort the integers in the array&nbsp;in ascending order by the number of&nbsp;1&#8217;s&nbsp;in their binary representation and in case&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,16,222,23],"class_list":["post-6353","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-bit","tag-easy","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6353"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6353\/revisions"}],"predecessor-version":[{"id":6356,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6353\/revisions\/6356"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}