[leetcode 16] 最接近的三数之和

给定一个包括n的整数数组nums,和一个目标值target。找出nums中的三个整数,使得它们的和与target最接近,返回这三个数的和。假定每次输入只存在一个答案。

示例:

nums = [-1,2,1,-4] 和 target = 1

与 target 最接近的三个数的和为2. (-1+2+1=2)

代码:

class Solution(object):
    def threeSumClosest(self,nums,target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """

         if not nums:
         return []

         sortNum=sorted[nums]
         closest=sys.maxint

         for i in range(len(sortNum)):
            left= i+1
            right=len(sortNum)-1
            while left < right:
               Sum = sortNum[i]+sortNum[left]+sortNum[right]
               if Sum == target:
                  return Sum
               elif: Sum > target:
                  right -=1
               else:
                  left +=1
               closest = Sum if abs(Sum - target) > abs (closest - target) else closest
          return closest

(参考链接:https://www.jianshu.com/p/0399dbefd67e)

原文地址:https://www.cnblogs.com/statlearning2019/p/10347816.html

时间: 2024-07-30 13:21:24

[leetcode 16] 最接近的三数之和的相关文章

LeetCode:最接近的三数之和【16】

LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 题目分析 这道题就是三数之和问题的一种变形. 三数之和问题的求解策略是将三指针变为双指针问题

[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

16.最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). class Solution {    public int threeSumClosest(int[] nums, int target) { 

16. 最接近的三数之和 O(N^2)

给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路就是先枚举一个数字,然后用尺取确定另外两个数字. class Solution { public: int threeSumClosest(v

两数之和,三数之和,最接近的三数之和,四数之和

LeetCode有一系列做法套路相同的题目,N数之和就可以算一个 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 第一个解决办法,简单暴力,堆for循环就是,但

LeetCode OJ: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(15):三数之和

Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ] 解题思路: 这道题让我们求三数之和,比之前那道Two Sum要复杂一些,考虑过先fix一个数,然后另外两个数使

LeetCode16.最接近的三数之和 JavaScript

给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 答案参考 /** * @param {number[]} nums * @param {number} target * @return {num

每天AC系列(二):最接近的三数之和

1 题目 leetcode第16题,给定一个数组与一个目标数,找出数组中其中的三个数,这三个数的和要与目标数最接近. 2 暴力 按惯例先来一次O(n3)的暴力: int temp = nums[0]+nums[1]+nums[2]; for(int i=0;i<nums.length;++i) for(int j=i+1;j<nums.length;++j) for(int k=j+1;k<nums.length;++k) { int temp1 = nums[i]+nums[j]+nu