Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b332699

Browse files
ybian19azl397985856
authored andcommitted
feat: 190.reverse-bits add Python3 implementation (azl397985856#112)
1 parent 26f5e44 commit b332699

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

‎problems/190.reverse-bits.md‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ https://leetcode.com/problems/reverse-bits/description/
66
```
77
Reverse bits of a given 32 bits unsigned integer.
88
9-
9+
1010
1111
Example 1:
1212
@@ -18,7 +18,7 @@ Example 2:
1818
Input: 11111111111111111111111111111101
1919
Output: 10111111111111111111111111111111
2020
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10101111110010110010011101101001.
21-
21+
2222
2323
Note:
2424
@@ -44,7 +44,7 @@ In Java, the compiler represents the signed integers using 2's complement notati
4444

4545
1. 可以用任何数字和1进行位运算的结果都取决于该数字最后一位的特性简化操作和提高性能
4646

47-
eg :
47+
eg :
4848

4949
- n & 1 === 1, 说明n的最后一位是1
5050
- n & 1 === 0, 说明n的最后一位是0
@@ -58,12 +58,11 @@ eg :
5858

5959
## 代码
6060

61-
* 语言支持:JS,C++
61+
* 语言支持:JS,C++,Python
6262

6363
JavaScript Code:
6464

6565
```js
66-
6766
/*
6867
* @lc app=leetcode id=190 lang=javascript
6968
*
@@ -135,7 +134,9 @@ var reverseBits = function(n) {
135134
return res >>> 0;
136135
};
137136
```
137+
138138
C++ Code:
139+
139140
```C++
140141
class Solution {
141142
public:
@@ -149,6 +150,21 @@ public:
149150
}
150151
};
151152
```
153+
154+
Python Code:
155+
156+
```python
157+
class Solution:
158+
# @param n, an integer
159+
# @return an integer
160+
def reverseBits(self, n):
161+
result = 0
162+
for i in range(32):
163+
result = (result << 1) + (n & 1)
164+
n >>= 1
165+
return result
166+
```
167+
152168
## 拓展
153169
不使用迭代也可以完成相同的操作:
154170
1. 两两相邻的1位对调

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /