Skip to content

Commit ab8fa34

Browse files
committed
二进制中有多少个1
1 parent bfa9662 commit ab8fa34

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

count_1_in_binary.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param num: an integer
5+
# @return: an integer, the number of ones in num
6+
def countOnes(self, num):
7+
# write your code here
8+
ret = 0
9+
num &= 0xffffffff
10+
while num != 0:
11+
if num & 1:
12+
ret += 1
13+
num >>= 1
14+
return ret

0 commit comments

Comments
 (0)