Skip to content

Commit 56231b8

Browse files
authored
Merge pull request #1985 from sonjh1217/main
[sonjh1217] WEEK 01 solutions
2 parents 9b7b419 + 7c372f4 commit 56231b8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

contains-duplicate/sonjh1217.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
// time O(n) / space O(n)
3+
func containsDuplicate(_ nums: [Int]) -> Bool {
4+
var numSet = Set<Int>()
5+
for num in nums {
6+
if numSet.contains(num) {
7+
return true
8+
}
9+
numSet.insert(num)
10+
}
11+
12+
return false
13+
}
14+
}

two-sum/sonjh1217.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
// time O(n) / space O(n)
3+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
4+
var indicesByCounters = [Int: Int]()
5+
for (i, num) in nums.enumerated() {
6+
if let index = indicesByCounters[num] {
7+
return [index, i]
8+
}
9+
10+
indicesByCounters[target - num] = i
11+
}
12+
13+
return []
14+
}
15+
}

0 commit comments

Comments
 (0)