We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9f0880c commit 0b7a4d1Copy full SHA for 0b7a4d1
reverse-bits/WhiteHyun.swift
@@ -0,0 +1,28 @@
1
+//
2
+// 190. Reverse Bits
3
+// https://leetcode.com/problems/reverse-bits/description/
4
+// Dale-Study
5
6
+// Created by WhiteHyun on 2024/05/19.
7
8
+
9
+final class Solution {
10
11
+ // MARK: - Runtime: 5ms / Memory 16.12 MB
12
13
+ func reverseBits(_ n: Int) -> Int {
14
+ let reversedStringBits = String(String(n, radix: 2).reversed())
15
+ return Int(reversedStringBits + String(repeating: "0", count: 32 - reversedStringBits.count), radix: 2)!
16
+ }
17
18
+ // MARK: - Runtime: 5ms / Memory 15.72 MB
19
20
+ func reverseBits2(_ n: Int) -> Int {
21
+ var answer = 0
22
23
+ for index in 0 ..< 32 {
24
+ answer += ((n >> (32 - index - 1)) & 1) << index
25
26
+ return answer
27
28
+}
0 commit comments