forked from soapyigu/LeetCode-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.
[Array] Add solutions to Two Sum II and III
- Loading branch information
Showing
3 changed files
with
67 additions
and
4 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,28 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ | ||
* Primary idea: Two pointers -- left moves forward and right moves backward to meet the right two sum. | ||
* | ||
* Time Complexity: O(n), Space Complexity: O(n) | ||
*/ | ||
|
||
class TwoSumII { | ||
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] { | ||
if numbers.count <= 1 { | ||
return [Int]() | ||
} | ||
|
||
var left = 0, right = numbers.count - 1 | ||
|
||
while left < right { | ||
if numbers[left] + numbers[right] < target { | ||
left += 1 | ||
} else if numbers[left] + numbers[right] > target { | ||
right -= 1 | ||
} else { | ||
return [left + 1, right + 1] | ||
} | ||
} | ||
|
||
return [Int]() | ||
} | ||
} |
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,33 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/two-sum-iii-data-structure-design/ | ||
* | ||
* Note: This answer offers a different solution instead of the one requsted by leetcode. | ||
* Assuming the use case is find API is called many more times than add API. | ||
* For the answer accepted by leetcode, you could reference Two Sum and Two Sum II. | ||
* | ||
* Primary idea: Use a set for all two sums value, and array to keep all numbers added. | ||
* Time Complexity: add - O(n), find - O(1), Space Complexity: O(n) | ||
*/ | ||
|
||
class TwoSumIII { | ||
|
||
var nums: [Int] | ||
var twoSums: Set<Int>() | ||
|
||
/** Initialize your data structure here. */ | ||
init() { | ||
nums = [Int]() | ||
twoSums = Set<Int>() | ||
} | ||
|
||
/** Add the number to an internal data structure.. */ | ||
func add(_ number: Int) { | ||
nums.forEach { twoSums.insert($0 + number) } | ||
nums.append(number) | ||
} | ||
|
||
/** Find if there exists any pair of numbers which sum is equal to the value. */ | ||
func find(_ value: Int) -> Bool { | ||
return twoSums.contains(value) | ||
} | ||
} |
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