Skip to content

Commit be8f4db

Browse files
committed
feat: reverse bits
1 parent 8ca1cc0 commit be8f4db

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

reverse-bits/minji-go.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Problem: https://leetcode.com/problems/reverse-bits/
3+
Description: Reverse bits of a given 32 bits unsigned integer
4+
Topics: Divide and Conquer, Bit Manipulation
5+
Time Complexity: O(1), Runtime 1ms
6+
Space Complexity: O(1), Memory 41.72MB
7+
*/
8+
public class Solution {
9+
public int reverseBits(int n) {
10+
long unsignedNum = n > 0 ? n : n + 2 * (long) Math.pow(2,31); //= Integer.toUnsignedLong()
11+
12+
int reversedNum = 0;
13+
for(int i=31; i>=0; i--){
14+
if(unsignedNum % 2 == 1) reversedNum += (long) Math.pow(2,i); //= (1<<i)
15+
unsignedNum/=2;
16+
}
17+
return reversedNum;
18+
}
19+
}

0 commit comments

Comments
 (0)