-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
48 lines (41 loc) · 1.23 KB
/
Contents.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
/// 1. Two Sum
/// 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.
import XCTest
/// Approach: Dictionary
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dict: [Int: Int] = [:]
for (idx, num) in nums.enumerated() {
let complement = target - num
if let complementIdx = dict[complement] {
return [idx, complementIdx]
}
dict[num] = idx
}
// each input would have exactly one solution
fatalError()
}
class Tests: XCTestCase {
func testExample() {
let nums = [2, 7, 11, 15]
let target = 9
let solution = twoSum(nums, target)
XCTAssertEqual(solution, [1, 0])
}
func testNotUseSameElement() {
let nums = [3, 2, 4]
let target = 6
let solution = twoSum(nums, target)
XCTAssertEqual(solution, [2, 1])
}
func testNumsHasSameElements() {
let nums = [2, 2, 4]
let target = 4
let solution = twoSum(nums, target)
XCTAssertEqual(solution, [1, 0])
}
}
Tests.defaultTestSuite.run()