/
FindIndicesArraySumTarget.swift
67 lines (55 loc) · 1.66 KB
/
FindIndicesArraySumTarget.swift
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
*/
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var bestCombination:[Int] = [0,0]
for (i, num) in nums.enumerated() {
if num < target {
for (j, numo) in nums.enumerated(){
if num + numo == target {
bestCombination[0] = num
bestCombination[1] = numo
}
}
}
}
return bestCombination
}
print(twoSum([2, 7, 11, 15], 9))
//https://github.com/diwu/LeetCode-Solutions-in-Swift/blob/master/Solutions/Solutions/Medium/Medium_001_Two_Sum.swift
//I don't like to return index 1 = 2 and index 2 = 7
func twoSum(numbers: [Int], target: Int) -> [Int] {
var hashMap = [Int: Int]()
var result = [Int]()
for i in 0..<numbers.count {
let numberToFind: Int = target - numbers[i]
if let numberToFindIndex = hashMap[numberToFind] {
result.append(numberToFindIndex + 1)
result.append(i + 1)
return result
} else {
hashMap[numbers[i]] = i
}
}
return result
}
print(twoSum(numbers: [2, 7, 11, 15], target: 9))
//Refactoring to return the numbers to sum instead of the indexes.
func twoSumRef(numbers: [Int], target: Int) -> [Int] {
var hashMap = [Int: Int]()
var result = [Int]()
for i in 0..<numbers.count {
let numberToFind: Int = target - numbers[i]
if let numberToFindIndex = hashMap[numberToFind] {
//why use the + 1 ???
result.append(numberToFindIndex)
result.append(i)
return result
} else {
hashMap[numbers[i]] = i
}
}
return result
}
print(twoSumRef(numbers: [2, 7, 11, 15], target: 9))