[LeetCode]题解(python):016-3Sum Closest

题目来源:

https://leetcode.com/problems/3sum-closest/



题意分析:

这道题目输入一个数组nums和一个数target,找出数组中三个数,使得他们的和最接近target,返回这三个数的和。



题目思路:

这道题目和上一题3Sum很像,所以也可以用类似的方法去解决这个问题。整个过程分成两步:

①数组排序;这步时间复杂度是(O(nlogn))。

②固定一个数,这步的时间复杂度是(O(n))。

③在剩下的数里面通过“夹逼定理”,找出两个数,使得三个数的和最接近target。这步时间复杂度是(O(n))

总的时间复杂度为(O(nlogn) + O(n)*O(n)) = (O(n^2))。

优化:在第三步的时候通过判断剩下的数中是否最小的两个数相加就大于或者最大两个数就小于target - 第一个数,如果是,则直接判断最小(大)两个数和②中的那个数的和是不是最接近的值。



代码(python):

 1 class Solution(object):
 2     def threeSumClosest(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: int
 7         """
 8         size = len(nums)
 9         if size < 3:
10             return 0
11         nums.sort()
12         i = 0 # fix the first index
13         ans = nums[0] + nums[1] + nums[size - 1] # ans is used to record the solution
14         while i < size - 2:
15             tmp = target - nums[i]
16             j = i + 1
17             k = size - 1
18             while j < k:
19                 if nums[j] + nums[k] == tmp:
20                     return target
21                 if nums[j] + nums[k] > tmp:
22                     if nums[j] + nums[j + 1] >= tmp:
23                         if nums[j] + nums[j + 1] - tmp < abs(ans - target):
24                             ans = nums[i] + nums[j] + nums[j + 1]
25                         break
26                     tmpans = nums[i] + nums[j] + nums[k]
27                     if tmpans - target < abs(ans - target):
28                         ans = tmpans
29                     k -= 1
30                 else:
31                     if nums[k] + nums[k - 1] <= tmp:
32                         if tmp - nums[k] -nums[k - 1] < abs(ans - target):
33                             ans = nums[i] + nums[k - 1] + nums[k]
34                         break
35                     tmpans = nums[i] + nums[j] + nums[k]
36                     if target - tmpans < abs(ans - target):
37                         ans = tmpans
38                     j += 1
39             i += 1
40             if ans == target:
41                 return target
42         return ans



转载请注明出处:http://www.cnblogs.com/chruny/p/4830175.html

时间: 2024-11-02 20:30:37

[LeetCode]题解(python):016-3Sum Closest的相关文章

LeetCode 016 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] 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. 分析:

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium 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

[LeetCode][Python]16: 3Sum Closest

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 16: 3Sum Closesthttps://oj.leetcode.com/problems/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

【LeetCode】016 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][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int

Java for LeetCode 016 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

题目 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

leetcode第16题--3Sum Closest

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

016 3SUM Closest

这道题与 015 3 SUM 基本思路一样 都是夹逼思想  复杂度 O(n*n) class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def threeSumClosest(self, nums, target): length = len(nums) nums = sorted(nums) ans = sum(nums[0:3]) i = 0 while i < leng