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

leetcode 3Sum Closest python的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

  2. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  3. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. LeetCode 3Sum Closest (Two pointers)

    题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. LeetCode——3Sum Closest

    Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  6. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

  7. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  8. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  9. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

随机推荐

  1. Tcl 简单介绍及特性

    [简单介绍|特性] l  简单介绍 Tcl是一门产生于80年代末的语言,和Python一样,她是用c开发出来的.假设说C/Java/C++/C#为编译型语言的话,那么Python.Perl和Tcl就是 ...

  2. 【Android Studio】studio学习系列(一) 从eclipse导入project

    Android google官方出的IDE android studio 一直都在走bate版本号,尽管如此,总认为它比ADT更加靠谱.所以我也想用studio来开发滴.可项目一直都是eclipse的 ...

  3. Invalid file permission Please regenerate them with cacaoadm create-keys --force

    1.服务器重启之后,启动cacao报错,提示无效的文件权限. [root@ldapserver bin]# ./cacaoadm start Invalid file permission: [/ho ...

  4. EC读书笔记系列之20:条款53、54、55

    条款53 不要轻忽编译器的警告 记住: ★严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取“无任何警告”的荣誉 ★不要过度依赖编译器的报警能力,∵不同的编译器对待事情的态度 ...

  5. java创建对象的几种常用方法

    java几种常见的创建对象的方法: 1.使用new关键字创建对象 2.利用java的反射机制,通过java.lang.Class或者java.lang.reflect.Constructor创建对象 ...

  6. WARNING [Project: :app] To shrink resources you must also enable ProGuard

    新版本的Android Gradle plugin中,对于resource有了更加一步的管理,可以把unused resource移除,不仅是自己工程,并且library里面也可以没有用到的,也可以移 ...

  7. Android _优雅实现元素间的分割线 (支持3.0以下)

    转:http://blog.csdn.net/lmj623565791/article/details/42407923 1.概述 话说,随着Android SDK版本的升级,很多控件增加了新的属性方 ...

  8. 带参数的URLconf

    我们在Django建立helloworld自定义页面创建的页面,只能算是一个静态页,发起一个请求,返回一个固定的值,并不能满足我们动态的需求.今天我们创建一个带参数的URLconf,根据参数展示不同的 ...

  9. OpenCV2.4.9+VS2012安装与配置

    需要下载并安装Visual Studio 2012 然后在OpenCV官网下载安装OpenCV2.4.9 for Windows,网址为http://opencv.org/downloads.html ...

  10. UIButton基础知识

    基本属性 1.frame;坐标:title:titlecolor:字体颜色:titleShadowColor:字体阴影:image:图片: backgroundImage:背景图片: 2.forsta ...