forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy path1_Two_Sum.swift
executable file
·51 lines (33 loc) · 1.08 KB
/
1_Two_Sum.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
//: Playground - noun: a place where people can play
//: Problem Link: https://leetcode.com/problems/two-sum/
import UIKit
public class Solution_1 {
public init() {
}
public func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var firstIndex = 0
var secondIndex = 0
var resultArr = [0, 0]
for (index, value) in nums.enumerated() {
let firstValue = value
for index2 in (index + 1)..<(nums.count) {
let secondValue = nums[index2]
let sum = firstValue + secondValue
if sum == target {
firstIndex = index
secondIndex = index2
resultArr = [firstIndex, secondIndex]
return resultArr
}
}
}
return resultArr
}
}
/*
// Solution test code for: #1 - Two Sum
let inputArr = [0,4,3,0]
let target = 0
var solution_1 : Solution_1 = Solution_1()
solution_1.twoSum(inputArr, target)
*/