将一列数按照每个数二进制数字1的个数排序,用python的count很方便
1 #1356
2 #Runtime: 38 ms (Beats 86.93%)
3 #Memory: 13.4 MB (Beats 63.83%)
4
5 class Solution(object):
6 def sortByBits(self, arr):
7 """
8 :type arr: List[int]
9 :rtype: List[int]
10 """
11 arr.sort(key=lambda x: (bin(x).count("1"), x))
12 return arr