力扣——3sum closest(最接近的三数之和)python 实现

题目描述:

中文:

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

英文:

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.

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        mindiff = 10000
        nums.sort()
        res = 0
        for i in range(len(nums)):
            left = i+1
            right = len(nums)-1
            while left < right:
                sum = nums[left] + nums[right] + nums[i]
                diff = abs(target - sum)
                if diff < mindiff:
                    mindiff = diff
                    res = sum
                if target == sum:
                    return sum
                elif sum < target:
                    left += 1
                else:
                    right -= 1                      

        return res

题目来源:力扣题库

原文地址:https://www.cnblogs.com/spp666/p/11559513.html

时间: 2024-08-30 14:44:47

力扣——3sum closest(最接近的三数之和)python 实现的相关文章

[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 nu

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

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

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

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

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

[Swift]LeetCode16. 最接近的三数之和 | 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 nu

[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

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) { 

每天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