Skip to content

Commit 30f5aae

Browse files
committed
Solution Two Sum
1 parent 8415ff6 commit 30f5aae

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

two-sum/doitduri.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
3+
let dict = nums.enumerated().reduce(into: [Int: Int]()) { initialValue, num in
4+
initialValue[num.element] = num.offset
5+
}
6+
7+
for startIndex in 0..<nums.count {
8+
let findValue = target - nums[startIndex]
9+
if let endIndex = dict[findValue], startIndex != endIndex {
10+
return [startIndex, endIndex]
11+
}
12+
}
13+
14+
return []
15+
}
16+
}

0 commit comments

Comments
 (0)