From 8451af1e7804e97cb1ba597de10e7277596e0fcb Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:13:08 +0800 Subject: [PATCH] =?UTF-8?q?Update=201356.=E6=A0=B9=E6=8D=AE=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E4=BA=8C=E8=BF=9B=E5=88=B6=E4=B8=8B1=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=9B=AE=E6=8E=92=E5=BA=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增python代码 --- .../1356.根据数字二进制下1的数目排序.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index a122df6f..b8b0245d 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -156,6 +156,17 @@ class Solution { ## Python ```python +class Solution: + def sortByBits(self, arr: List[int]) -> List[int]: + arr.sort(key=lambda num: (self.count_bits(num), num)) + return arr + + def count_bits(self, num: int) -> int: + count = 0 + while num: + num &= num - 1 + count += 1 + return count ``` ## Go