Skip to content

[seungriyou] Week 08 Solutions #1504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions reverse-bits/seungriyou.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# https://leetcode.com/problems/reverse-bits/

class Solution:
def reverseBits_32(self, n: int) -> int:
"""
[Complexity]
- TC: O(32)
- SC: O(1)

[Approach]
n의 맨 오른쪽 bit부터 res의 맨 왼쪽에 붙여나가기
"""
res = 0
for i in range(32):
res |= ((n >> i) & 1) << (31 - i)
return res

def reverseBits(self, n: int) -> int:
"""
[Complexity]
- TC: O(16)
- SC: O(1)

[Approach]
n의 바깥쪽에서부터 two pointer 처럼 res에 모으기
"""
res = 0
for i in range(16):
left = (n >> (31 - i)) & 1
right = (n >> i) & 1
res |= (left << i) | (right << (31 - i))
return res