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. if len(nums) <= 2:
  9. return False
  10. nums.sort()
  11. res=nums[0]+nums[1]+nums[2]
  12. for i in range(len(nums)-2):
  13. left=i+1
  14. right=len(nums)-1
  15. while left < right:
  16. cur=nums[i]+nums[left]+nums[right]
  17.  
  18. if abs(cur-target) < abs(res-target):
  19. res=cur
  20. if res == target:
  21. return res
  22. elif cur > target:
  23. right-=1
  24. else:
  25. left+=1
  26.  
  27. 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. 依赖注入及AOP简述(四)——“好莱坞原则”和依赖注入框架简介 .

    3.2.    “好莱坞原则” 看了前面关于依赖注入概念的描述,我们来提炼出依赖注入的核心思想.如果说传统的组件间耦合方式,例如new.工厂模式等,是一种由开发者主动去构建依赖对象的话,那么依赖注入模 ...

  2. 【floyd求最小环】【Vijos 1046】【观光旅游】

    标签:图结构 最短路 题目大意:给你一个无向图,至少经过3个节点的简单回路(不能包括其他环) 一开始的思路:用一个NUM[i][j]表示i到j的最短路经过几个节点,显然解法不太优美,而且还是错的 再想 ...

  3. JAVA 和 C# 调用外部.exe文件,传值并等等exe完成,获取返回值

    JAVA- String ykexe = getProperty("ykexe") + " " + tableout; //getproperty(" ...

  4. jQuery关于mouseover和mouseenter的区别

    原生的mouseenter是dom3级的事件,对于jQuery等一些框架已经实现了这个事件.但是它到底跟mouseover有什么区别? jQuery在实现这两个事件的时候,mouseover支持事件冒 ...

  5. 标准C++的vector使用

    原文:http://blog.csdn.net/pandy1110/article/details/5963908 C++内置的数组支持容器的机制,但是它不支持容器抽象的语义.要解决此问题我们自己实现 ...

  6. 浅谈:配置本地yum源(centos)

    删除YUM的所有配置信息[root@server yum.repos.d]#rm -rf * 现在手动配置:1.在根目录下创建文件夹centos-yum: [root@server /]#mkdir ...

  7. NT kernel & System 占用占用80端口

    问题: 1 运行'netstat -ano'发现80端口被pid=4的进程占用 2 打开任务管理器,发现pid=4的进程,其实是system进程,其对应的进程描述是NT kernel & sy ...

  8. hibernate异常

    <h1> nested exception is org.hibernate.LazyInitializationException:</h1> stackoverflow:h ...

  9. bash基础知识

    站在用户登录的角度来说,SHELL的类型:登录式shell: 正常通常某终端登录 su - USERNAME su -l USERNAME 非登录式shell: su USERNAME 图形终端下打开 ...

  10. DataTable中执行DataTable.Select("条件"),

    我们在使用Sql ******这些数据库时,可以轻松的通过Sum.Aver.Count等统计出相关结果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Serv ...