Skip to content

[Hyun] Week 4 Solution Explanation #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions counting-bits/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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)
}

}
18 changes: 18 additions & 0 deletions group-anagrams/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
30 changes: 30 additions & 0 deletions missing-number/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 24 additions & 0 deletions number-of-1-bits/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
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
}
}