forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathekgns33.java
More file actions
39 lines (31 loc) · 773 Bytes
/
ekgns33.java
File metadata and controls
39 lines (31 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
input : 32 bit unsigned integer n
output : unsigned integer representation of reversed bit
constraint :
1) input is always 32 bit unsigned integer
2) implementation should not be affected by programming language
solution 1)
get unsigned integer bit representation
build string s O(n)
reverse O(n)
get integer O(n)
tc : O(n) sc : O(n)
solution 2) one loop
nth bit indicates (1<<n) if reverse (1<< (32 - n))
add up in one loop
return
tc : O(n), sc: O(1)
*/
public class Solution {
// you need treat n as an unsigned value
static final int LENGTH = 32;
public int reverseBits(int n) {
int answer = 0;
for(int i = 0; i < LENGTH; i++) {
if(((1<<i) & n) != 0) {
answer += (1 << (LENGTH - i - 1));
}
}
return answer;
}
}