[LeetCode] 016. 3Sum Closest (Medium) (C++/Java/Python)
2015-03-03 19:10
435 查看
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode
代码(github):https://github.com/illuz/leetcode
015. 3Sum (Medium) 类似,甚至更简单。
还是先排序,再左右夹逼。
Java:
Python:
Github: https://github.com/illuz/leetcode
016.3Sum_Closest (Medium)
链接:
题目:https://oj.leetcode.com/problems/3sum-closest/代码(github):https://github.com/illuz/leetcode
题意:
在给定数列中找出三个数,使和最接近 target。分析:
与015. 3Sum (Medium) 类似,甚至更简单。
还是先排序,再左右夹逼。
代码:
C++:class Solution { public: int threeSumClosest(vector<int> &num, int target) { int ret = num[0] + num[1] + num[2]; int len = num.size(); sort(num.begin(), num.end()); for (int i = 0; i <= len - 3; i++) { // first number : num[i] int j = i + 1; // second number int k = len - 1; // third number while (j < k) { int sum = num[i] + num[j] + num[k]; if (abs(sum - target) < abs(ret - target)) ret = sum; if (sum < target) { ++j; } else if (sum > target) { --k; } else { ++j; --k; } } } return ret; } };
Java:
public class Solution { public int threeSumClosest(int[] num, int target) { Arrays.sort(num); int ret = num[0] + num[1] + num[2]; int len = num.length; for (int i = 0; i <= len - 3; i++) { // first number : num[i] int j = i + 1; // second number int k = len - 1; // third number while (j < k) { int sum = num[i] + num[j] + num[k]; if (Math.abs(sum - target) < Math.abs(ret - target)) ret = sum; if (sum < target) { ++j; } else if (sum > target) { --k; } else { ++j; --k; } } } return ret; } }
Python:
class Solution: # @return an integer def threeSumClosest(self, num, target): if not len(num): return 0 ret = num[0] + num[1] + num[2] num.sort() for i in range(len(num) - 2): j = i + 1 k = len(num) - 1 while j < k: tsum = num[i] + num[j] + num[k] if abs(tsum - target) < abs(ret - target): ret = tsum if tsum < target: j += 1 elif tsum > target: k -= 1 else: j += 1 k -= 1 return ret
相关文章推荐
- [LeetCode] 018. 4Sum (Medium) (C++/Java/Python)
- [LeetCode] 002. Add Two Numbers (Medium) (C++/Java/Python)
- [LeetCode] 012. Integer to Roman (Medium) (C++/Java/Python)
- [LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)
- [LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)
- [LeetCode] 022. Generate Parentheses (Medium) (C++/Java/Python)
- [LeetCode] 015. 3Sum (Medium) (C++/Java/Python)
- [LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)
- [LeetCode] 001. Two Sum (Medium) (C++/Java/Python)
- [LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)
- LeetCode 149 — Max Points on a Line(C++ Java Python)
- LeetCode 26 — Remove Duplicates from Sorted Array(C++ Java Python)
- LeetCode 63 — Unique Paths II(C++ Java Python)
- LeetCode 142 — Linked List Cycle II(C++ Java Python)
- LeetCode 69 — Sqrt(x)(C++ Java Python)
- LeetCode 15 — 3Sum(C++ Java Python)
- LeetCode 70 — Climbing Stairs(C++ Java Python)
- LeetCode 141 — Linked List Cycle(C++ Java Python)
- LeetCode 62 — Unique Paths(C++ Java Python)
- LeetCode题解汇总(C++ Java Python,含题目翻译)