1. # -*- coding: utf8 -*-
    '''
    __author__ = 'dabay.wang@gmail.com'
  2.  
  3. 15: 3Sum
    https://oj.leetcode.com/problems/3sum/
  4.  
  5. Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
    Find all unique triplets in the array which gives the sum of zero.
  6.  
  7. Note:
    Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
    The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},
  8.  
  9. A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)
  10.  
  11. ===Comments by Dabay===
    先排序,然后从左到右固定一个数,在后边的数列中使用左右指针往中间靠拢的方法查找。
  12.  
  13. 从左往右固定一个数(如果需要判断的数与之前的相等就跳过),
    左右两个指针往中间靠拢的时候,能够保证不遗漏。(如果找到一组数据之后,左指针继续移动到下一个数不相等的数)
    '''
    class Solution:
    # @return a list of lists of length 3, [[val1,val2,val3]]
    def threeSum(self, num):
    if len(num) < 3:
    return []
    num.sort()
    res = []
    for i in xrange(len(num)-2):
    if i>0 and num[i]==num[i-1]:
    continue
    target = 0 - num[i]
    j, k = i+1, len(num)-1
    while j < k:
    if num[j] + num[k] == target:
    res.append([num[i], num[j], num[k]])
    j = j + 1
    while j<k and num[j]==num[j-1]:
    j = j + 1
    elif num[j] + num[k] < target:
    j = j + 1
    else:
    k = k - 1
    return res
  14.  
  15. def main():
    sol = Solution()
    nums = [0,0,0,0,0]
    solutions = sol.threeSum(nums)
    for solution in solutions:
    print solution
  16.  
  17. if __name__ == "__main__":
    import time
    start = time.clock()
    main()
    print "%s sec" % (time.clock() - start)

[LeetCode][Python]15:3Sum的更多相关文章

  1. LeetCode(15)3Sum

    题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...

  2. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  3. leetcode第15题--3Sum

    Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...

  4. LeetCode(15) 3Sum

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  5. [LeetCode]题解(python):002-Add Two Numbers

    题目来源: https://leetcode.com/problems/add-two-numbers/ 题意分析: 这道题目是要将两个单链条相加.输出得到的新链条. 题目思路: 不难发现,其实题目就 ...

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

    题目来源: https://leetcode.com/problems/3sum-closest/ 题意分析: 这道题目输入一个数组nums和一个数target,找出数组中三个数,使得他们的和最接近t ...

  7. [LeetCode]题解(python):015-3Sum

    题目来源: https://leetcode.com/problems/3sum/ 题意分析: 这道题目是输入一个数组nums.找出所有的3个数使得这3个数之和为0.要求1.输出的3个数按小到大排序, ...

  8. [LeetCode]题解(python):018-4Sum

    题目来源: https://leetcode.com/problems/4sum/ 题意分析: 这道题目和3Sum的题目类似,找出所有的4个数,使得这4个数等于target. 题目思路: 这道题做法和 ...

  9. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

随机推荐

  1. css实现两端对齐的3种方法

    两端对齐在移动端非常见,说到两端对齐,大家并不陌生,在word.powerpoint.outlook等界面导航处,其实都有一个两端对齐(分散对齐)的按钮,平时使用的也不多,我们更习惯与左对齐.居中对齐 ...

  2. 获取自动增长IDmysqli函数

    <?php $mysqli=@new mysqli("localhost", "root", "123456", "xsph ...

  3. mysql性能优化学习笔记(3)常见sql语句优化

    一.max()优化mysql> explain select max(payment_date) from payment;+----+-------------+---------+----- ...

  4. cocos2d-x box2d使用调试绘图

    cocos2d-x box2d使用调试绘图 复制TestCpp的GLES-Render.h和GLES-Render.cpp过来. 添加一个成员变量: GLESDebugDraw *m_debugDra ...

  5. aspose.words 处理word转PDF

    处理如下: import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.platform.cust ...

  6. 自动注册服务NET Core扩展IServiceCollection

    NET Core扩展IServiceCollection自动注册服务 前言 在ASP.NET Core中使用依赖注入中使用很简单,只需在Startup类的ConfigureServices()方法中, ...

  7. RBF network

    1.radial basis function RBF表示某种距离,$\mu_m$为中心点,相当于将点$x$到中心点的某种距离作为特征转换 Output方法可以根据需求任意选取(比如使用SVM,log ...

  8. hdu 4740

    题目链接 老虎左拐,老鼠右拐,碰到不能走的拐一次,如果还不能走就停下,自己走过的不能走,求相遇的坐标或-1 一个停下之后,另一个还可以走 #include <cstdio> #includ ...

  9. 配置启动挂载:fstab文件具体解释

    fstab文件介绍 fstab文件包括了你的电脑上的存储设备及其文件系统的信息.它是决定一个硬盘(分区)被如何使用或者说整合到整个系统中的文件. 详细来说:用fstab能够自己主动挂载各种文件系统格式 ...

  10. CSS ::before 和 ::after 伪元素用法

    CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...