Skip to content

Commit a501932

Browse files
committedDec 5, 2020
feat: reverse-bits
1 parent 56c02be commit a501932

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

Diff for: ‎bin.power-of-two.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
"""
3+
231. 2的幂
4+
https://leetcode-cn.com/problems/power-of-two/
5+
给定一个整数,编写一个函数来判断它是否是 2 的幂次方。
6+
"""
7+
def isPowerOfTwo(self, n: int) -> bool:
8+
return n != 0 and n & (n - 1) == 0
9+
10+
11+
so = Solution()
12+
print(so.isPowerOfTwo(15))

Diff for: ‎bin.reverse-bits.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
"""
3+
190. 颠倒二进制位
4+
https://leetcode-cn.com/problems/reverse-bits/
5+
颠倒给定的 32 位无符号整数的二进制位。
6+
"""
7+
def reverseBits(self, n: int) -> int:
8+
res = 0
9+
for i in range(32):
10+
res = (res << 1) + (n & 1)
11+
n = n >> 1
12+
13+
return res
14+
15+
16+
so = Solution()
17+
print(so.reverseBits(0o11111111111111111111111111111101))

0 commit comments

Comments
 (0)
Please sign in to comment.