Skip to content

Commit d35be7d

Browse files
author
changmuk.im
committed
solve : week 2 with swift
1 parent 27f04bc commit d35be7d

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed

counting-bits/EGON.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import Foundation
2+
3+
class Solution {
4+
func countBits(_ n: Int) -> [Int] {
5+
return solve_2(n)
6+
}
7+
8+
/*
9+
**내장함수를 사용한 풀이**
10+
11+
Runtime: 37 ms (Beats 49.12%)
12+
Time Complexity: O(n * log n)
13+
- 길이가 n인 range를 순회하므로 O(n)
14+
- 정수에 대해 non zero bit를 세는데 O(log n)
15+
> O(n * log n)
16+
Space Complexity: O(n)
17+
- 배열에 값을 저장하며 크기가 n이 되므로 O(n)
18+
Memory: 20.86 MB (Beats 90.106%)
19+
*/
20+
func solve_1(_ n: Int) -> [Int] {
21+
var result: [Int] = []
22+
for num in 0...n {
23+
result.append(num.nonzeroBitCount)
24+
}
25+
return result
26+
}
27+
28+
/*
29+
** Brian Kernighan's Algorithm을 사용한 풀이 **
30+
31+
Runtime: 39 ms (Beats 42.11%)
32+
Time Complexity: O(n * log n)
33+
- 길이가 n인 range를 순회하므로 O(n)
34+
- bitCountWithBrianKernighan 에서 내부 while문 실행마다 num이 절반으로 줄어드므로, 실행횟수는 O(log n)
35+
> O(n * log n)
36+
Space Complexity: O(n)
37+
- 배열에 값을 저장하며 크기가 n이 되므로 O(n)
38+
Memory: 16.01 MB (Beats 67.84%)
39+
*/
40+
func solve_2(_ n: Int) -> [Int] {
41+
42+
func bitCountWithBrianKernighan(_ num: Int) -> Int {
43+
var num = num
44+
var bitCount = 0
45+
while 0 < num {
46+
num &= (num - 1)
47+
bitCount += 1
48+
}
49+
50+
return bitCount
51+
}
52+
53+
var result: [Int] = []
54+
for num in 0...n {
55+
result.append(bitCountWithBrianKernighan(num))
56+
}
57+
return result
58+
}
59+
60+
/*
61+
** MSB에 대해 DP를 사용한 풀이 **
62+
> num의 비트 카운트 = MSB의 비트 카운트(1 고정) + (num - msb)의 비트 카운트
63+
64+
Runtime: 36 ms (Beats 58.48%)
65+
Time Complexity: O(n)
66+
- 0부터 n까지 range를 순회하므로 O(n)
67+
> O(n)
68+
Space Complexity: O(n)
69+
- 길이가 n인 dp 배열을 저장하므로 O(n)
70+
Memory: 21.15 MB (Beats 67.84%)
71+
*/
72+
func solve_3(_ n: Int) -> [Int] {
73+
guard 1 <= n else { return [0] }
74+
75+
var dp = [Int](repeating: 0, count: n + 1)
76+
var msb = 1
77+
for num in 1...n {
78+
if msb << 1 == num {
79+
msb = num
80+
}
81+
82+
dp[num] = 1 + dp[num - msb]
83+
}
84+
85+
return dp
86+
}
87+
88+
/*
89+
** LSB에 대해 DP를 사용한 풀이 **
90+
> num의 비트 카운트 = num을 right shift한 숫자의 비트 카운트 + LSB의 비트 카운트(1 또는 0)
91+
92+
Runtime: 28 ms (Beats 97.08%)
93+
Time Complexity: O(n)
94+
- 0부터 n까지 range를 순회하므로 O(n)
95+
> O(n)
96+
Space Complexity: O(n)
97+
- 길이가 n인 dp 배열을 저장하므로 O(n)
98+
Memory: 21.26 MB (Beats 53.80%)
99+
*/
100+
func solve_4(_ n: Int) -> [Int] {
101+
guard 1 <= n else { return [0] }
102+
103+
var dp = [Int](repeating: 0, count: n + 1)
104+
for num in 1...n {
105+
dp[num] = dp[num >> 1] + (num & 1)
106+
}
107+
108+
return dp
109+
}
110+
}
111+
112+
let solution = Solution()
113+
print(solution.solve_4(0))

encode-and-decode-strings/EGON.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Foundation
2+
3+
class Solution {
4+
static let DELIMITER = ","
5+
6+
/* ASCII 풀이 */
7+
static let ASCII_STR_LENGTH = 3
8+
9+
func encodeWithASCII(strs: [String]) -> String {
10+
return strs.map { (str) in
11+
str.compactMap { (char) in
12+
guard let asciiValue = char.asciiValue else {
13+
return nil
14+
}
15+
16+
let asciiStr = String(asciiValue)
17+
18+
return String(
19+
repeating: "0",
20+
count: Solution.ASCII_STR_LENGTH - asciiStr.count
21+
) + asciiStr
22+
}.reduce("") { (acc, cur) in acc + cur }
23+
}
24+
.joined(separator: Solution.DELIMITER)
25+
}
26+
27+
func decodeWithASCII(str: String) -> [String] {
28+
return str.components(separatedBy: Solution.DELIMITER)
29+
.map { (encodedStr) in
30+
return stride(from: 0, to: encodedStr.count, by: Solution.ASCII_STR_LENGTH)
31+
.compactMap { i in
32+
let startIndex = String.Index(utf16Offset: i, in: encodedStr)
33+
let endIndex = String.Index(utf16Offset: i + Solution.ASCII_STR_LENGTH, in: encodedStr)
34+
let asciiValue = UInt8(encodedStr[startIndex..<endIndex])
35+
return asciiValue
36+
}
37+
}.compactMap { String(bytes: $0, encoding: .ascii) }
38+
}
39+
40+
/* BASE64 풀이 */
41+
func encodeWithBASE64(strs: [String]) -> String {
42+
return strs.compactMap { (str) in
43+
if let data = str.data(using: .utf8) {
44+
return data.base64EncodedString()
45+
} else {
46+
return nil
47+
}
48+
}.joined(separator: Solution.DELIMITER)
49+
}
50+
51+
func decodeWithBASE64(str: String) -> [String] {
52+
return str.components(separatedBy: Solution.DELIMITER)
53+
.compactMap { (encodedStr) in
54+
if let data = Data(base64Encoded: encodedStr) {
55+
return String(data: data, encoding: .utf8)
56+
} else {
57+
return nil
58+
}
59+
}
60+
}
61+
}
62+
63+
let solution = Solution()
64+
let strs = ["lint", "code", "love", "you"]
65+
let encoded = solution.encodeWithBASE64(strs: strs)
66+
print(encoded)
67+
let decoded = solution.decodeWithBASE64(str: encoded)
68+
print(decoded)

valid-anagram/EGON.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
3+
/*
4+
Runtime: 16 ms (Beats 84.96%)
5+
Time Complexity: O(n)
6+
- 길이가 n인 str s, str t, dict counter를 순회하므로 O(3n) ~= O(n)
7+
Space Complexity: O(n)
8+
- 크기가 최대 n인 dict를 변수로 저장하여 사용하므로 O(n)
9+
Memory: 16.01 MB (Beats 97.18%)
10+
*/
11+
func isAnagram(_ s: String, _ t: String) -> Bool {
12+
var countDict: [String.Element: Int] = [:]
13+
14+
for char in s {
15+
countDict[char] = (countDict[char] ?? 0) + 1
16+
}
17+
18+
for char in t {
19+
guard let count = countDict[char] else {
20+
return false
21+
}
22+
23+
countDict[char] = count - 1
24+
}
25+
26+
for count in countDict.values {
27+
if count != 0 {
28+
return false
29+
}
30+
}
31+
32+
return true
33+
}
34+
}

0 commit comments

Comments
 (0)