Leetcode 3Sum Closest (C,c++,java,python)

Source: Internet
Author: User

Problem:

Given an array S of n integers, find three integers in S such so the sum is closest to a give n number, target. Return the sum of the three integers. You may assume this each input would has exactly one solution.

    For example, given array S = {-1 2 1-4}, and target = 1.    The sum is closest to the target is 2. (-1 + 2 + 1 = 2).
Solution: This question and 15 basic similar, even more simple, only need to compare and the result can, encounter and equal to target when you directly return it!!!
To give an array of integers, find the one with the shortest distance of three numbers and the value with the given target.
Problem-solving ideas: Look directly at the code, not read my 15 questions
Java source code (spents 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 Language Source code (spents 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 + + source code (spents 12ms):
Class Solution {Public:int threesumclosest (vector<int>& nums, int target) {int length=nums.size (), Mi        n=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 source code (spents 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


Leetcode 3Sum Closest (C,c++,java,python)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.