[LeetCode] 1. Two Sum [Easy]
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dictionary:[Int: Int] = [:]
for i in 0..<nums.count {
let value = target - nums[i];
if(dictionary[value] != nil) {
return [dictionary[value]!, i]
}
dictionary[nums[i]] = i;
}
return [];
}
}Last updated