LeetCode 16 — 3Sum Closest(最接近的三數之和)
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
翻譯
給定一個包括 n 個整數的陣列 nums 和 一個目標值 target。找出 nums 中的三個整數,使得它們的和與 target 最接近。返回這三個數的和。假定每組輸入只存在唯一答案。
例如,給定陣列 nums = [-1,2,1,-4], 和 target = 1.
與 target 最接近的三個數的和為 2. (-1 + 2 + 1 = 2).
分析
這題其實比3Sum還要簡單,方法相同,由於只有一組解,所以不用考慮重複。
c++實現
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res = 0;
int tmp = 0;
if (nums.size() <= 3)
{
for (int i = 0; i < nums.size(); i++)
res += nums[i];
return res;
}
sort(nums.begin(),nums.end());
res = nums[0]+nums[1]+nums[2];
for (int i = 0; i < nums.size()-2; i++)
{
int j = i+1;
int k = nums.size()-1;
while (j < k)
{
tmp = nums[i]+nums[j]+nums[k];
if (abs(target-tmp) <= abs(target-res))
res = tmp;
if (tmp < target)
j++;
else if (tmp > target)
k--;
else
break;
}
}
return res;
}
};
相關推薦
leetcode-16:3sum closest最近的三數之和
題目: Given an array nums of n integers and an integer target, find three integers in nums such that t
[LeetCode]16. 3Sum Closest最接近的三數之和
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the su
LeetCode 16 — 3Sum Closest(最接近的三數之和)
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return
【leetcode】#陣列【Python】16. 3Sum Closest 最接近的三數之和
連結: 題目: 給定一個包括 n 個整數的陣列 nums 和 一個目標值 target。找出 nums 中的三個整數,使得它們的和與 target 最接近。返回這三個數的和。假定每組輸入只存在唯一答
LeetCode 3Sum Closest 最接近目標數的三個數和
3Sum Closest Given an arraySofnintegers, find three integers inSsuch that the sum is closest t
[LeetCode] 3Sum Closest 最近的三數之和 Python
3Sum Closest: 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
[LeetCode] 16. 3Sum Closest 最近三數之和
tor || sum max int public each href .com Given an array S of n integers, find three integers in S such that the sum is closest to a given
[LeetCode] 16. 3Sum Closest Java
leetcode i+1 each find ould for log temp 之前 題目:Given an array S of n integers, find three integers in S such that the sum is closest to a
LeetCode(16)-3Sum Closest
16. 3Sum Closest Given an array nums of n integers and an integer target, find three >integers in nums such that the sum is closest to
leetcode-16. 3Sum Closest
類似15題和18題的結合 題意: 給出一個數組和一個目標值,找出陣列中的餓三個數,使他們的加和最接近target,輸出和。(注意不是輸出解) 我的思路: 陣列排序 外層迴圈找第一個數 low = i + 1,high = len - 1
#Leetcode# 16. 3Sum Closest
https://leetcode.com/problems/3sum-closest/ Given an array nums of n integers and an integer target, find three integers
python leetcode 16. 3Sum Closest
方法與3Sum類似 class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: i
LeetCode 16 3Sum Closest(C,C++,Java,Python)
Problem: Given an array S of n integers, find three integers in S such that the sum is closest to
Leetcode 16 3Sum Closest
Given an array nums of n integers and an integer target, find three integers in nums such that the su
LeetCode 16. 3Sum Closest(給定和,求三元組)
題目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a g
LeetCode(16) 3Sum Closest
class Solution { public: int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(), nums.en
leetcode --16. 3Sum Closest
題目:https://leetcode.com/problems/3sum-closest/description/ 程式碼:class Solution { public: int threeSumClosest(vector<int>& nu
演算法分析與設計——LeetCode:16. 3Sum Closest
class Solution { public: int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(), nums.end()); int first, seco
leetCode#16. 3Sum Closest
Description Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Retur
Leetcode演算法Java全解答--015. 三數之和
Leetcode演算法Java全解答–015. 三數之和 題目 給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ? 找出所有滿足條件且不重複的三元組。 注意:答案中不可以包含重複的三元組。 示例