Skip to content

Commit d8628e0

Browse files
author
Yi Gu
committed
[Array] add Solution to Four Sum and Summary Ranges
1 parent bca209e commit d8628e0

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

Array/FourSum.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/4sum/
3+
* Primary idea: Sort the array, and traverse it, increment left or decrease right
4+
* predicated on their sum is greater or not than the target
5+
* Time Complexity: O(n^3), Space Complexity: O(nC4)
6+
*/
7+
8+
class FourSum {
9+
func fourSum(nums: [Int], _ target: Int) -> [[Int]] {
10+
let nums = nums.sort({$0 < $1})
11+
var threeSum = 0
12+
var twoSum = 0
13+
var left = 0
14+
var right = 0
15+
var res = [[Int]]()
16+
17+
guard nums.count >= 4 else {
18+
return res
19+
}
20+
21+
for i in 0 ..< nums.count - 3 {
22+
guard i == 0 || nums[i] != nums[i - 1] else {
23+
continue
24+
}
25+
threeSum = target - nums[i]
26+
27+
for j in i + 1 ..< nums.count - 2 {
28+
guard j == i + 1 || nums[j] != nums[j - 1] else {
29+
continue
30+
}
31+
twoSum = threeSum - nums[j]
32+
33+
left = j + 1
34+
right = nums.count - 1
35+
while left < right {
36+
if nums[left] + nums[right] == twoSum {
37+
res.append([nums[i], nums[j], nums[left], nums[right]])
38+
repeat {
39+
left += 1
40+
} while left < right && nums[left] == nums[left - 1]
41+
repeat {
42+
right -= 1
43+
} while left < right && nums[right] == nums[right + 1]
44+
} else if nums[left] + nums[right] < twoSum {
45+
repeat {
46+
left += 1
47+
} while left < right && nums[left] == nums[left - 1]
48+
} else {
49+
repeat {
50+
right -= 1
51+
} while left < right && nums[right] == nums[right + 1]
52+
}
53+
}
54+
}
55+
}
56+
57+
return res
58+
}
59+
}

Array/SummaryRanges.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/summary-ranges/
3+
* Primary idea: Traverse the array and build string when num[i] != num[i - 1] + 1,
4+
* note to handle the edge case when it goes to the end of the array
5+
*
6+
* Time Complexity: O(n), Space Complexity: O(n)
7+
*/
8+
9+
class SummaryRanges {
10+
func summaryRanges(nums: [Int]) -> [String] {
11+
var res = [String]()
12+
var str = ""
13+
var start = 0
14+
15+
guard nums.count > 0 else {
16+
return res
17+
}
18+
19+
for i in 0 ... nums.count {
20+
if i == nums.count || (i > 0 && nums[i] != nums[i - 1] + 1) {
21+
str = "\(nums[start])"
22+
if i - 1 != start {
23+
str += "->\(nums[i - 1])"
24+
}
25+
res.append(str)
26+
start = i
27+
}
28+
}
29+
return res
30+
}
31+
}

Array/ThreeSum.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/3sum/
33
* Primary idea: Sort the array, and traverse it, increment left or decrease right
44
* predicated on their sum is greater or not than the target
5-
* Time Complexity: O(n^2), Space Complexity: O(n)
5+
* Time Complexity: O(n^2), Space Complexity: O(nC3)
66
*/
77

88
class ThreeSum {

0 commit comments

Comments
 (0)