We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8ca1cc0 commit be8f4dbCopy full SHA for be8f4db
reverse-bits/minji-go.java
@@ -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