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 a given number, target. Return the sum of the three integers. You may assume that each input would have
exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Solution:

此题与15题基本类似,甚至更简单一些,只需要比较和的结果即可,碰到和等于target的时候就直接返回吧!!!

题目大意:

给一个整数数组,找到三个数的和与给定target的值距离最短的那个和

解题思路:

直接看代码把,没看懂的看我的15题题解

Java源代码(用时342ms):

public class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int length=nums.length,Min=Integer.MAX_VALUE;
        Arrays.sort(nums);
        for(int i=0;i<length-2;i++){
            if(i>0 && nums[i]==nums[i-1])continue;
            int begin=i+1,end=length-1;
            while(begin<end){
                int sum=nums[i]+nums[begin]+nums[end];
                if(Math.abs(sum-target)<Math.abs(Min))Min=sum-target;
                if(sum==target)return target;
                else if(sum>target)end--;
                else begin++;
            }
        }
        return Min+target;
    }
}

C语言源代码(用时9ms):

int abs(int tar){
    return tar>0?tar:-tar;
}
void quickSort(int* nums,int first,int end){
    int l=first,r=end;
    if(first>=end)return;
    int temp=nums[l];
    while(l<r){
        while(l<r && nums[r]>=temp)r--;
        if(l<r)nums[l]=nums[r];
        while(l<r && nums[l]<=temp)l++;
        if(l<r)nums[r]=nums[l];
    }
    nums[l]=temp;
    quickSort(nums,first,l-1);
    quickSort(nums,l+1,end);
}
int threeSumClosest(int* nums, int numsSize, int target) {
    int begin,end,i,sum,Min=INT_MAX;
    quickSort(nums,0,numsSize-1);
    for(i=0;i<numsSize-2;i++){
        if(i>0 && nums[i]==nums[i-1])continue;
        begin=i+1;end=numsSize-1;
        while(begin<end){
            sum=nums[i]+nums[begin]+nums[end];
            if(abs(sum-target)<abs(Min))Min=sum-target;
            if(sum==target)return target;
            else if(sum>target)end--;
            else begin++;
        }
    }
    return Min+target;
}

C++源代码(用时12ms):

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int length=nums.size(),Min=2147483647;
        quickSort(nums,0,length-1);
        for(int i=0;i<length-2;i++){
            if(i>0 && nums[i]==nums[i-1])continue;
            int begin=i+1,end=length-1;
            while(begin<end){
                int sum=nums[i]+nums[begin]+nums[end];
                if(abs(sum-target)<abs(Min))Min=sum-target;
                if(sum==target)return target;
                else if(sum>target)end--;
                else begin++;
            }
        }
        return Min+target;
    }
private:
    int abs(int t){
        return t>0?t:-t;
    }
    void quickSort(vector<int>& nums,int first,int end){
        int l=first,r=end,tmp;
        if(first>=end)return;
        tmp=nums[l];
        while(l<r){
            while(l<r && nums[r]>=tmp)r--;
            if(l<r)nums[l]=nums[r];
            while(l<r && nums[l]<=tmp)l++;
            if(l<r)nums[r]=nums[l];
        }
        nums[l]=tmp;
        quickSort(nums,first,l-1);
        quickSort(nums,l+1,end);
    }
};

Python源代码(用时127ms):

class Solution:
    # @param {integer[]} nums
    # @param {integer} target
    # @return {integer}
    def threeSumClosest(self, nums, target):
        length=len(nums);Min=2147483647
        nums.sort()
        for i in range(length-2):
            if i>0 and nums[i]==nums[i-1]:continue
            begin=i+1;end=length-1
            while begin<end:
                sum=nums[i]+nums[begin]+nums[end]
                if abs(sum-target)<abs(Min):Min=sum-target
                if sum==target:return target
                elif sum>target:end-=1
                else:begin+=1
        return Min+target
时间: 2024-10-17 07:07:03

LeetCode 16 3Sum Closest(C,C++,Java,Python)的相关文章

[LeetCode] 016. 3Sum Closest (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 016.3Sum_Closest (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum-closest/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和最接近 target. 分析:

Leetcode 16. 3Sum Closest(指针搜索)

16. 3Sum Closest Medium 131696FavoriteShare 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 e

leetCode 16.3Sum Closest (离给定值最近的三数之和) 解题思路和方法

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 sum of the three integers. You may assume that each input would have exactly one solution. For example, given arr

Java [leetcode 16] 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 sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S =

[LeetCode] 16. 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 sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

LeetCode #16 3Sum Closest (M)

[Problem] 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. For example, given array

【LeetCode】3Sum Closest 解题报告 (Java)

[题目] 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. For example, given array S = {

Array + two points 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. 给定数组,找出并返回最接近target的

LeetCode 16 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 sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2