1. 程式人生 > >leetcode --16. 3Sum Closest

leetcode --16. 3Sum Closest

題目:https://leetcode.com/problems/3sum-closest/description/

程式碼:

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        if(nums.size() < 3) return 0;
        sort(nums.begin(), nums.end());
        int closet = nums[0]+nums[1]+nums[2];
        for(int i =0;i<nums.size()-2;i++){
            if(i>0&&nums[i]==nums[i-1]) continue;
            int start = i+1,end = nums.size()-1;
            while(start<end){
                int sum = nums[i]+nums[start]+nums[end];
                if(sum == target) return sum;
                if(abs(target-closet)>abs(sum-target)){
                    closet = sum;
                }
                if(sum>target) end--;
                if(sum<target) start++;
            }
        }
        return closet;
    }
};

相關推薦

[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 最近三數之和

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

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最近的三數之和

題目: Given an array nums of n integers and an integer target, find three integers in nums such that t

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 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 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 163Sum 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 --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

LeetCode16. 3Sum Closest - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given an array nums of n integers and an integer target, find three inte

leetcode16 3Sum Closest

描述 給定一個數字集合 S 以及一個數字 target,需要從集合中找出3個數字的和與這個 target的值最接近(絕對值最小) 樣例 Input: S = [-1, 2, 1, -4], target = 1 Output: 2 思路 首先排序,之後確定一個數字的前提下,再利用雙指標從兩端

LeetCode16. 3Sum Closest(C++)

題目: Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. R

leetcode題解-15. 3Sum && 16. 3Sum Closest

15,題目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the