forked from diwu/LeetCode-Solutions-in-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Di Wu
committed
Feb 12, 2015
0 parents
commit e8e3fb0
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='3.0' sdk='iphonesimulator'> | ||
<sections> | ||
<code source-file-name='section-1.swift'/> | ||
</sections> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
https://oj.leetcode.com/problems/two-sum/ | ||
#1 Two Sum | ||
Given an array of integers, find two numbers such that they add up to a specific target number. | ||
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. | ||
You may assume that each input would have exactly one solution. | ||
Input: numbers={2, 7, 11, 15}, target=9 | ||
Output: index1=1, index2=2 | ||
Inspired by @naveed.zafar at https://oj.leetcode.com/discuss/10947/accepted-c-o-n-solution | ||
*/ | ||
|
||
import Foundation | ||
|
||
// O(N) | ||
func twoSum(numbers: Array<Int>, target: Int) -> Array<Int> { | ||
var hashMap: Dictionary = Dictionary<Int, Int>() | ||
var result: Array = Array<Int>() | ||
|
||
for i in 0..<numbers.count { | ||
var 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 | ||
} | ||
|
||
// test cases | ||
twoSum([2, 7, 11, 15], 9) | ||
twoSum([5, 5], 10) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |