aboutsummaryrefslogtreecommitdiff
path: root/sort_int_1bits.py
diff options
context:
space:
mode:
Diffstat (limited to 'sort_int_1bits.py')
-rw-r--r--sort_int_1bits.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/sort_int_1bits.py b/sort_int_1bits.py
new file mode 100644
index 0000000..861b388
--- /dev/null
+++ b/sort_int_1bits.py
@@ -0,0 +1,33 @@
+# 1356. Sort Integers by The Number of 1 Bits
+
+"""
+given an integer array 'arr', sort the integers in the array in ascending
+order by the number of 1's in their binary representation and in case of two
+or more integers have the same number of 1's, you have to sort them in
+ascending order. return the array after sorting it.
+"""
+
+
+class Solution(object):
+ def sortByBits(self, arr):
+ n = len(arr)
+ ans = [0] * n
+ for i in range(n):
+ ans[i] = self.count_bit(arr[i]) * 10001 + arr[i]
+ ans.sort()
+ for i in range(n):
+ ans[i] %= 10001
+ return ans
+
+ def count_bit(self, n):
+ res = 0
+ while n != 0:
+ res += n & 1
+ n >>= 1
+ return res
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.sortByBits([0, 1, 2, 3, 4, 5, 6, 7, 8]))
+ print(obj.sortByBits([1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]))