Skip to content

Commit

Permalink
Add solution for "234. Reverse Bits"
Browse files Browse the repository at this point in the history
  • Loading branch information
KwonNayeon committed Dec 27, 2024
1 parent a3f13cf commit 58d4dd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 24 additions & 0 deletions reverse-bits/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Constraints:
- The input must be a binary string of length 32
Time Complexity: O(1)
- ํ•ญ์ƒ ๊ณ ์ •๋œ 32๋น„ํŠธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ์—ฐ์‚ฐํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ์‹œ๊ฐ„
Space Complexity: O(1)
- 32๋น„ํŠธ ๊ณ ์ • ํฌ๊ธฐ์˜ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ๊ณต๊ฐ„
ํ’€์ด ๋ฐฉ๋ฒ•:
1. format(n, '032b')๋ฅผ ์‚ฌ์šฉํ•ด ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋ฅผ 32๋น„ํŠธ ์ด์ง„์ˆ˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•จ
2. ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ [::-1]์œผ๋กœ ๋น„ํŠธ๋ฅผ ๋’ค์ง‘์Œ
3. int(reversed_binary, 2)๋กœ ๋’ค์ง‘์€ ์ด์ง„์ˆ˜ ๋ฌธ์ž์—ด์„ ๋‹ค์‹œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•จ
"""

class Solution:
def reverseBits(self, n: int) -> int:

binary = format(n, '032b')

reversed_binary = binary[::-1]

return int(reversed_binary, 2)
6 changes: 2 additions & 4 deletions two-sum/KwonNayeon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
- -10^9 <= target <= 10^9
- Only one valid answer exists.
Time Complexity: O(nยฒ)
Time Complexity: O(nยฒ)
- ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ
- ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ: n๋ฒˆ
- ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋‘ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ: n-1, n-2, ... 1๋ฒˆ
- ๋”ฐ๋ผ์„œ ์ด ์—ฐ์‚ฐ ํšŸ์ˆ˜๋Š” n * (n-1)/2๋กœ O(nยฒ)
Space Complexity: O(1)
Space Complexity: O(1)
- ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
- result ๋ฆฌ์ŠคํŠธ๋Š” ํ•ญ์ƒ ํฌ๊ธฐ๊ฐ€ 2๋กœ ๊ณ ์ •
"""
Expand All @@ -23,5 +23,3 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
for j in range(i+1, len(nums)):
if nums[j] == target - nums[i]:
return [i, j]


0 comments on commit 58d4dd3

Please sign in to comment.