Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions reverse-bits/prograsshopper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int reverseBits(int n) {
int[] stack_n = new int[32];
int i = 0;
while(i < 32){
stack_n[i] = n % 2;
n /= 2;
i++;
}

int result = 0;
int scale = 1;

for(int j=31;j>0;j--){
result += stack_n[j] * scale;
scale *= 2;
}

return result;
}
}
15 changes: 15 additions & 0 deletions reverse-bits/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def reverseBits(self, n: int) -> int:
stack_n = []
while len(stack_n) < 32:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while len(stack_n) < 32:
for _ in range(32):

while문 대신 for문 이용하시면 좀 더 가독성이 좋을 것 같아서 이런 방식도 고려해보시면 좋을 것 같습니다!

stack_n.append((n % 2))
n //= 2

result = 0
scale = 1

while stack_n:
result += stack_n.pop() * scale
scale *= 2

return result