Skip to content

Commit dc33916

Browse files
authored
Merge pull request #90 from WhiteHyun/whitehyun/week4
[Hyun] Week 4 Solution Explanation
2 parents e86ad1e + 16eb8a0 commit dc33916

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

counting-bits/WhiteHyun.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// 338. Counting Bits
3+
// https://leetcode.com/problems/counting-bits/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/19.
7+
//
8+
9+
final class Solution {
10+
11+
// MARK: - Time Complexity: O(n), Space Complexity: O(n)
12+
13+
func countBits(_ n: Int) -> [Int] {
14+
var array: [Int] = .init(repeating: 0, count: n + 1)
15+
for i in stride(from: 1, through: n, by: 1) {
16+
array[i] = array[i >> 1] + (i & 1)
17+
}
18+
return array
19+
}
20+
21+
// MARK: - nonzeroBitCount
22+
23+
func countBits2(_ n: Int) -> [Int] {
24+
return (0...n).map(\.nonzeroBitCount)
25+
}
26+
27+
}

group-anagrams/WhiteHyun.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// 49. Group Anagrams
3+
// https://leetcode.com/problems/group-anagrams/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/19.
7+
//
8+
9+
final class Solution {
10+
func groupAnagrams(_ strs: [String]) -> [[String]] {
11+
var dictionary: [String: [String]] = [:]
12+
for str in strs {
13+
dictionary[String(str.sorted()), default: []].append(str)
14+
}
15+
16+
return Array(dictionary.values)
17+
}
18+
}

missing-number/WhiteHyun.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 268. Missing Number
3+
// https://leetcode.com/problems/missing-number/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/19.
7+
//
8+
9+
final class Solution {
10+
func missingNumber(_ nums: [Int]) -> Int {
11+
(0 ... nums.count).reduce(0, +) - nums.reduce(0, +)
12+
}
13+
14+
func missingNumber2(_ nums: [Int]) -> Int {
15+
nums.count * (nums.count + 1) / 2 - nums.reduce(0, +)
16+
}
17+
18+
func missingNumber3(_ nums: [Int]) -> Int {
19+
Set(0...nums.count).subtracting(Set(nums)).first!
20+
}
21+
22+
func missingNumber4(_ nums: [Int]) -> Int {
23+
var answer = 0
24+
for i in 0 ..< nums.count {
25+
answer = answer ^ i ^ nums[i]
26+
}
27+
28+
return answer ^ nums.count
29+
}
30+
}

number-of-1-bits/WhiteHyun.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// 191. Number of 1 Bits
3+
// https://leetcode.com/problems/number-of-1-bits/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/19.
7+
//
8+
9+
final class Solution {
10+
func hammingWeight(_ n: Int) -> Int {
11+
n.nonzeroBitCount
12+
}
13+
14+
func hammingWeight2(_ n: Int) -> Int {
15+
var number = n
16+
var answer = 0
17+
while number != 0 {
18+
answer += number & 1
19+
number >>= 1
20+
}
21+
22+
return answer
23+
}
24+
}

reverse-bits/WhiteHyun.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)