Skip to content

Commit 28bb79a

Browse files
committed
[LeetCode Sync] Runtime - 37 ms (71.04%), Memory - 17.8 MB (25.85%)
1 parent f55d5eb commit 28bb79a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<p>Reverse bits of a given 32 bits signed integer.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">n = 43261596</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">964176192</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<table>
14+
<tbody>
15+
<tr>
16+
<th>Integer</th>
17+
<th>Binary</th>
18+
</tr>
19+
<tr>
20+
<td>43261596</td>
21+
<td>00000010100101000001111010011100</td>
22+
</tr>
23+
<tr>
24+
<td>964176192</td>
25+
<td>00111001011110000010100101000000</td>
26+
</tr>
27+
</tbody>
28+
</table>
29+
</div>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">n = 2147483644</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">1073741822</span></p>
37+
38+
<p><strong>Explanation:</strong></p>
39+
40+
<table>
41+
<tbody>
42+
<tr>
43+
<th>Integer</th>
44+
<th>Binary</th>
45+
</tr>
46+
<tr>
47+
<td>2147483644</td>
48+
<td>01111111111111111111111111111100</td>
49+
</tr>
50+
<tr>
51+
<td>1073741822</td>
52+
<td>00111111111111111111111111111110</td>
53+
</tr>
54+
</tbody>
55+
</table>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>0 &lt;= n &lt;= 2<sup>31</sup> - 2</code></li>
63+
<li><code>n</code> is even.</li>
64+
</ul>
65+
66+
<p>&nbsp;</p>
67+
<p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def reverseBits(self, n: int) -> int:
3+
result = 0
4+
for i in range(32):
5+
result |= (n & 1) << (31 - i)
6+
n >>= 1
7+
return result

0 commit comments

Comments
 (0)