Skip to content

Commit 5657d19

Browse files
committed
Leetcode 16
Python Solution
1 parent 9044b1d commit 5657d19

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages!!
1919
| 13 | Easy | [Roman to Integer](leetcode/13-Easy-Roman-To-Integer/problem.md) | [C](leetcode/13-Easy-Roman-To-Integer/answer.c) |
2020
| 14 | Easy | [Longest Common Prefix](leetcode/14-Easy-Longest-Common-Prefix/problem.md) | [Python](leetcode/14-Easy-Longest-Common-Prefix/answer.py) |
2121
| 15 | Medium | [3Sum](leetcode/15-Medium-3Sum/problem.md) | [Python](leetcode/15-Medium-3Sum/answer.py) |
22+
| 16 | Medium | [3Sum Closest](leetcode/16-Medium-3Sum-Closest/problem.md) | [Python](leetcode/16-Medium-3Sum-Closest/answer.py) |
2223
| 20 | Easy | [Valid Parentheses](leetcode/20-Easy-Valid-Parentheses/problem.md) | [Java](leetcode/20-Easy-Valid-Parentheses/answer.java) |
2324
| 21 | Easy | [Merge Two Sorted Lists](leetcode/21-Easy-Merge-Two-Sorted-Lists/problem.md) | [Java](leetcode/21-Easy-Merge-Two-Sorted-Lists/answer.java) |
2425
| 22 | Medium | [Generate Parentheses](leetcode/22-Medium-Generate-Parentheses/problem.md) | [Python](leetcode/22-Medium-Generate-Parentheses/answer.py) |

Diff for: leetcode/16-Medium-3Sum-Closest/answer.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python
2+
3+
#------------------------------------------------------------------------------
4+
5+
class Solution:
6+
def threeSumClosest(self, nums, target):
7+
"""
8+
:type nums: List[int]
9+
:type target: int
10+
:rtype: int
11+
"""
12+
13+
nums.sort()
14+
15+
result = float('inf')
16+
17+
# Loop through each index, then use two pointers to close in the rest of the array
18+
for i in range(len(nums)-2):
19+
l, r = i+1, len(nums)-1
20+
21+
while l < r:
22+
# Current sum of the three elements
23+
curr_sum = nums[i] + nums[l] + nums[r]
24+
# If the difference is smaller, make it the result
25+
if abs(target - curr_sum) < abs(target - result):
26+
result = curr_sum
27+
28+
if curr_sum == target:
29+
return curr_sum
30+
elif curr_sum < target:
31+
l += 1
32+
else:
33+
r -= 1
34+
35+
return result
36+
37+
#------------------------------------------------------------------------------
38+
#Testing
39+
40+
41+
42+
main()

Diff for: leetcode/16-Medium-3Sum-Closest/problem.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Problem 16: 3Sum Closest
2+
3+
4+
#### Difficulty: Medium
5+
6+
#### Problem
7+
8+
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
9+
10+
#### Example
11+
12+
<pre>
13+
14+
For example, given array S = {-1 2 1 -4}, and target = 1.
15+
16+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
17+
18+
</pre>

0 commit comments

Comments
 (0)