Skip to content

Commit 9b8351c

Browse files
committed
3Sum Closest.
1 parent d80c05b commit 9b8351c

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

Medium/15.3Sum.playground/Contents.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Solution {
3838
if sortedNums[i] > 0 {
3939
break
4040
}
41-
// Remove duplicate elements.
41+
// filter duplicate elements.
4242
if i > 0 && sortedNums[i] == sortedNums[i - 1] {
4343
continue
4444
}
@@ -48,7 +48,7 @@ class Solution {
4848
if sortedNums[i] + sortedNums[left] + sortedNums[right] == 0 {
4949
res.append([sortedNums[i], sortedNums[left], sortedNums[right]])
5050

51-
// Remove duplicate elements.
51+
// filter duplicate elements.
5252
while left < right && sortedNums[right] == sortedNums[right - 1] {
5353
right -= 1
5454
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
3+
4+
Return the sum of the three integers.
5+
6+
You may assume that each input would have exactly one solution.
7+
8+
 
9+
10+
Example 1:
11+
Input: nums = [-1,2,1,-4], target = 1
12+
Output: 2
13+
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
14+
15+
Example 2:
16+
Input: nums = [0,0,0], target = 1
17+
Output: 0
18+
 
19+
20+
Constraints:
21+
- 3 <= nums.length <= 1000
22+
- -1000 <= nums[i] <= 1000
23+
- -10^4 <= target <= 10^4
24+
*/
25+
class Solution {
26+
func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
27+
let sortedNums = nums.sorted(by: <)
28+
var closestNum = sortedNums[0] + sortedNums[1] + sortedNums[2]
29+
for i in 0..<sortedNums.count - 2 {
30+
var left = i + 1
31+
var right = sortedNums.count - 1
32+
while left < right {
33+
let sumThree = sortedNums[i] + sortedNums[left] + sortedNums[right]
34+
if abs(sumThree - target) < abs(closestNum - target) {
35+
closestNum = sumThree
36+
}
37+
if sumThree > target {
38+
right -= 1
39+
} else if sumThree < target {
40+
left += 1
41+
} else {
42+
return target
43+
}
44+
}
45+
}
46+
return closestNum
47+
}
48+
}
49+
50+
let s = Solution()
51+
let r = s.threeSumClosest([0, 1, 2], 3)
52+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Medium/16.3Sum Closest.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,4 @@
114114
7. [Container With Most Water](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/11.Container%20With%20Most%20Water.playground/Contents.swift)
115115
8. [Integer to Roman](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/12.Integer%20to%20Roman.playground/Contents.swift)
116116
9. [3Sum](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/15.3Sum.playground/Contents.swift)
117+
10. [3Sum Closest](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/16.3Sum%20Closest.playground/Contents.swift)

0 commit comments

Comments
 (0)