diff --git a/counting-bits/WhiteHyun.swift b/counting-bits/WhiteHyun.swift new file mode 100644 index 000000000..2eca4a743 --- /dev/null +++ b/counting-bits/WhiteHyun.swift @@ -0,0 +1,27 @@ +// +// 338. Counting Bits +// https://leetcode.com/problems/counting-bits/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/19. +// + +final class Solution { + + // MARK: - Time Complexity: O(n), Space Complexity: O(n) + + func countBits(_ n: Int) -> [Int] { + var array: [Int] = .init(repeating: 0, count: n + 1) + for i in stride(from: 1, through: n, by: 1) { + array[i] = array[i >> 1] + (i & 1) + } + return array + } + + // MARK: - nonzeroBitCount + + func countBits2(_ n: Int) -> [Int] { + return (0...n).map(\.nonzeroBitCount) + } + +} diff --git a/group-anagrams/WhiteHyun.swift b/group-anagrams/WhiteHyun.swift new file mode 100644 index 000000000..18c0ef782 --- /dev/null +++ b/group-anagrams/WhiteHyun.swift @@ -0,0 +1,18 @@ +// +// 49. Group Anagrams +// https://leetcode.com/problems/group-anagrams/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/19. +// + +final class Solution { + func groupAnagrams(_ strs: [String]) -> [[String]] { + var dictionary: [String: [String]] = [:] + for str in strs { + dictionary[String(str.sorted()), default: []].append(str) + } + + return Array(dictionary.values) + } +} diff --git a/missing-number/WhiteHyun.swift b/missing-number/WhiteHyun.swift new file mode 100644 index 000000000..0c9f7120e --- /dev/null +++ b/missing-number/WhiteHyun.swift @@ -0,0 +1,30 @@ +// +// 268. Missing Number +// https://leetcode.com/problems/missing-number/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/19. +// + +final class Solution { + func missingNumber(_ nums: [Int]) -> Int { + (0 ... nums.count).reduce(0, +) - nums.reduce(0, +) + } + + func missingNumber2(_ nums: [Int]) -> Int { + nums.count * (nums.count + 1) / 2 - nums.reduce(0, +) + } + + func missingNumber3(_ nums: [Int]) -> Int { + Set(0...nums.count).subtracting(Set(nums)).first! + } + + func missingNumber4(_ nums: [Int]) -> Int { + var answer = 0 + for i in 0 ..< nums.count { + answer = answer ^ i ^ nums[i] + } + + return answer ^ nums.count + } +} diff --git a/number-of-1-bits/WhiteHyun.swift b/number-of-1-bits/WhiteHyun.swift new file mode 100644 index 000000000..53c06d4d7 --- /dev/null +++ b/number-of-1-bits/WhiteHyun.swift @@ -0,0 +1,24 @@ +// +// 191. Number of 1 Bits +// https://leetcode.com/problems/number-of-1-bits/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/19. +// + +final class Solution { + func hammingWeight(_ n: Int) -> Int { + n.nonzeroBitCount + } + + func hammingWeight2(_ n: Int) -> Int { + var number = n + var answer = 0 + while number != 0 { + answer += number & 1 + number >>= 1 + } + + return answer + } +} diff --git a/reverse-bits/WhiteHyun.swift b/reverse-bits/WhiteHyun.swift new file mode 100644 index 000000000..768b3db93 --- /dev/null +++ b/reverse-bits/WhiteHyun.swift @@ -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 + } +}