首页 > 其他 > 详细

leetcode16

时间:2019-06-28 23:34:49      阅读:100      评论:0      收藏:0      [点我收藏+]
 1 import sys
 2 class Solution:
 3     def threeSumClosest(self, nums: List[int], target: int) -> int:
 4         n = len(nums)
 5         if n <= 3:
 6             return sum(nums)
 7         nums = sorted(nums)
 8         mindiff = sys.maxsize
 9         minsums = 0
10         for i in range(n-2):
11             j,k = i + 1,n - 1
12             while j < k:
13                 sums = nums[i] + nums[j] + nums[k]
14                 diff = sums - target
15                 if diff == 0:
16                     return target
17                 elif diff < 0:
18                     j += 1
19                 else:
20                     k -= 1
21                 if abs(diff) < mindiff:
22                     mindiff = abs(diff)
23                     minsums = sums
24         return minsums

 

leetcode16

原文:https://www.cnblogs.com/asenyang/p/11105099.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!