Skip to content

Commit c77ae22

Browse files
committed
[Array] Add solutions to Two Sum II and III
1 parent 1c51685 commit c77ae22

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

Array/TwoSumII.swift

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
3+
* Primary idea: Two pointers -- left moves forward and right moves backward to meet the right two sum.
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*/
7+
8+
class TwoSumII {
9+
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
10+
if numbers.count <= 1 {
11+
return [Int]()
12+
}
13+
14+
var left = 0, right = numbers.count - 1
15+
16+
while left < right {
17+
if numbers[left] + numbers[right] < target {
18+
left += 1
19+
} else if numbers[left] + numbers[right] > target {
20+
right -= 1
21+
} else {
22+
return [left + 1, right + 1]
23+
}
24+
}
25+
26+
return [Int]()
27+
}
28+
}

Array/TwoSumIII.swift

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/two-sum-iii-data-structure-design/
3+
*
4+
* Note: This answer offers a different solution instead of the one requsted by leetcode.
5+
* Assuming the use case is find API is called many more times than add API.
6+
* For the answer accepted by leetcode, you could reference Two Sum and Two Sum II.
7+
*
8+
* Primary idea: Use a set for all two sums value, and array to keep all numbers added.
9+
* Time Complexity: add - O(n), find - O(1), Space Complexity: O(n)
10+
*/
11+
12+
class TwoSumIII {
13+
14+
var nums: [Int]
15+
var twoSums: Set<Int>()
16+
17+
/** Initialize your data structure here. */
18+
init() {
19+
nums = [Int]()
20+
twoSums = Set<Int>()
21+
}
22+
23+
/** Add the number to an internal data structure.. */
24+
func add(_ number: Int) {
25+
nums.forEach { twoSums.insert($0 + number) }
26+
nums.append(number)
27+
}
28+
29+
/** Find if there exists any pair of numbers which sum is equal to the value. */
30+
func find(_ value: Int) -> Bool {
31+
return twoSums.contains(value)
32+
}
33+
}

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[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.
7+
[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.
88

99
## Contributors
1010

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

0 commit comments

Comments
 (0)