Skip to content

Commit

Permalink
[Array] Add solutions to Two Sum II and III
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Dec 27, 2019
1 parent 1c51685 commit c77ae22
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
28 changes: 28 additions & 0 deletions Array/TwoSumII.swift
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]()
}
}
33 changes: 33 additions & 0 deletions Array/TwoSumIII.swift
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)
}
}
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 304 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 306 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -61,6 +61,8 @@
[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)| [Swift](./Array/StrobogrammaticNumber.swift)| Easy| O(n)| O(1)|
[Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)| [Swift](./Array/CanPlaceFlowers.swift)| Easy| O(n)| O(1)|
[Two Sum](https://leetcode.com/problems/two-sum/)| [Swift](./Array/TwoSum.swift)| Easy| O(n)| O(n)|
[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)| [Swift](./Array/TwoSumII.swift)| Easy| O(n)| O(1)|
[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/)| [Swift](./Array/TwoSumIII.swift)| Easy| O(n)| O(1)|
[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)| [Swift](./Array/TwoSumLessThanK.swift)| Easy| O(nlogn)| O(n)|
[3Sum](https://leetcode.com/problems/3sum/)| [Swift](./Array/ThreeSum.swift)| Medium| O(n^2)| O(nC3)|
[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [Swift](./Array/ThreeSumClosest.swift)| Medium| O(n^2)| O(nC3)|
Expand Down Expand Up @@ -713,10 +715,10 @@
| | 173 | [Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | Medium |
| [Swift](./Math/FactorialTrailingZeroes.swift) | 172 | [Factorial Trailing Zeroes](https://oj.leetcode.com/problems/factorial-trailing-zeroes/) | Easy |
| [Swift](./Math/ExcelSheetColumnNumber.swift) | 171 | [Excel Sheet Column Number](https://oj.leetcode.com/problems/excel-sheet-column-number/) | Easy |
| | 170 | [Two Sum III - Data structure design](https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/) &hearts; | Easy |
| [Swift](./Array/MajorityElement.swift) | 169 | [Majority Element](https://oj.leetcode.com/problems/majority-element/) | Easy |
| [Swift](./Array/TwoSumIII.swift) | 170 | [Two Sum III - Data structure design](https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/) &hearts; | Easy |
| [Swift](./Array/MajorityElement.swift) | 169 | [Majority Element](https://oj.leetcode.com/problems/majority-element/) | Easy |
| | 168 | [Excel Sheet Column Title](https://oj.leetcode.com/problems/excel-sheet-column-title/) | Easy |
| | 167 | [Two Sum II - Input array is sorted](https://oj.leetcode.com/problems/two-sum-ii-input-array-is-sorted/) &hearts; | Medium |
| [Swift](./Array/TwoSumII.swift) | 167 | [Two Sum II - Input array is sorted](https://oj.leetcode.com/problems/two-sum-ii-input-array-is-sorted/) &hearts; | Medium |
| [Swift](./Math/FractionToRecurringDecimal.swift) | 166 | [Fraction to Recurring Decimal](https://oj.leetcode.com/problems/fraction-to-recurring-decimal/) | Medium |
| | 165 | [Compare Version Numbers](https://oj.leetcode.com/problems/compare-version-numbers/) | Easy |
| | 164 | [Maximum Gap](https://oj.leetcode.com/problems/maximum-gap/) | Hard |
Expand Down

0 comments on commit c77ae22

Please sign in to comment.