1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/problems/swift/two-sum.swift
Matej Focko 333866d1bc
chore: split solutions by language
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-02 17:19:02 +02:00

15 lines
343 B
Swift

class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var encountered: [Int: Int] = [:]
for (i, x) in nums.enumerated() {
if encountered[target - x] != nil {
return [encountered[target - x]!, i]
}
encountered[x] = i
}
return []
}
}