-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add solution for LeetCode problem 190
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// 190. Reverse Bits | ||
// https://leetcode.com/problems/reverse-bits/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/05/19. | ||
// | ||
|
||
final class Solution { | ||
|
||
// MARK: - Runtime: 5ms / Memory 16.12 MB | ||
|
||
func reverseBits(_ n: Int) -> Int { | ||
let reversedStringBits = String(String(n, radix: 2).reversed()) | ||
return Int(reversedStringBits + String(repeating: "0", count: 32 - reversedStringBits.count), radix: 2)! | ||
} | ||
|
||
// MARK: - Runtime: 5ms / Memory 15.72 MB | ||
|
||
func reverseBits2(_ n: Int) -> Int { | ||
var answer = 0 | ||
|
||
for index in 0 ..< 32 { | ||
answer += ((n >> (32 - index - 1)) & 1) << index | ||
} | ||
return answer | ||
} | ||
} |