Skip to content

Commit ddc8958

Browse files
committed
将整数A转换为B
1 parent 98c144f commit ddc8958

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

flip_bits.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
"""
5+
@param a, b: Two integer
6+
return: An integer
7+
"""
8+
def bitSwapRequired(self, a, b):
9+
# write your code here
10+
a ^= b
11+
if a > 0:
12+
return self.ones(a)
13+
else:
14+
return 32 - self.ones(abs(a) - 1)
15+
16+
def ones(self, n):
17+
ret = 0
18+
n &= 0xffffffff
19+
while n != 0:
20+
if n & 1:
21+
ret += 1
22+
n >>= 1
23+
return ret

0 commit comments

Comments
 (0)