-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.swift
More file actions
35 lines (30 loc) · 1.06 KB
/
twoSum.swift
File metadata and controls
35 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/two-sum/
/*
Two Sum
분류 : Array
문제 설명 :
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
입출력 예 1:
nums = [2, 7, 11, 15], target = 9, return [0, 1]
입출력 예 2:
nums = [3, 2, 4], target = 6, return [1, 2]
입출력 예 3:
nums = [3, 3], target = 6, return [0, 1]
Time complexity : O(n)
Space complexity : O(n)
*/
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var indexTable = [Int: Int]()
for (currentIndex, currentNumber) in nums.enumerated() {
let requiredNumber = target - currentNumber
if let targetIndex = indexTable[requiredNumber] {
return [currentIndex, targetIndex]
}
indexTable[currentNumber] = currentIndex
}
return []
}
}