Skip to content

Commit 18ed4fa

Browse files
Merge pull request #1905 from prograsshopper/main
[prograsshopper] Week 8 Solution
2 parents 7e129ba + be01a39 commit 18ed4fa

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

reverse-bits/prograsshopper.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int reverseBits(int n) {
3+
int[] stack_n = new int[32];
4+
int i = 0;
5+
while(i < 32){
6+
stack_n[i] = n % 2;
7+
n /= 2;
8+
i++;
9+
}
10+
11+
int result = 0;
12+
int scale = 1;
13+
14+
for(int j=31;j>0;j--){
15+
result += stack_n[j] * scale;
16+
scale *= 2;
17+
}
18+
19+
return result;
20+
}
21+
}

reverse-bits/prograsshopper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def reverseBits(self, n: int) -> int:
3+
stack_n = []
4+
while len(stack_n) < 32:
5+
stack_n.append((n % 2))
6+
n //= 2
7+
8+
result = 0
9+
scale = 1
10+
11+
while stack_n:
12+
result += stack_n.pop() * scale
13+
scale *= 2
14+
15+
return result

0 commit comments

Comments
 (0)