File tree Expand file tree Collapse file tree 2 files changed +74
-0
lines changed
Solutions/0190-reverse-bits Expand file tree Collapse file tree 2 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+ <p >Reverse bits of a given 32 bits signed integer.</p >
2+
3+ <p >  ; </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 >  ; </p >
59+ <p ><strong >Constraints:</strong ></p >
60+
61+ <ul >
62+ <li><code>0 <= n <= 2<sup>31</sup> - 2</code></li>
63+ <li><code>n</code> is even.</li>
64+ </ul >
65+
66+ <p >  ; </p >
67+ <p ><strong >Follow up:</strong > If this function is called many times, how would you optimize it?</p >
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments