Skip to content

Commit

Permalink
feat: Add solution for LeetCode problem 190
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteHyun committed May 24, 2024
1 parent 9f0880c commit 0b7a4d1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions reverse-bits/WhiteHyun.swift
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
}
}

0 comments on commit 0b7a4d1

Please sign in to comment.