算法题

问题描述:
在一些给定的数中,找到三个数,他们相加的和是0,并且这三个数的组合是不能重复的

例子:

input

[-1, 0, -1, 2, 1]

Output

[[-1, 1 ,0], [-1, -1, 2]]

解法:

Python代码:

Class threesum(object):
  def sumthree(self, nums):
    nums.sort() #先对nums数组进行排序,这里的nums是一个list变量

  Numlist = []

  for i in range(len(nums) -2):
    if i !=0 and nums[i] == nums[i-1]:

      Continue

    J,k = i+1, len(nums)-1

    While j <k:

      S = nums[i] +nums[j] + nums[k]

      If s>0 or (k < len(nums) -1 and nums[k ] == nums[k+1])

         K -= 1

      Elif s<0 or (k>i +1 and nums[j] == nums[j -1])

          J -= 1

      Else:

        Numliist.append([nums[i], nums[j], nums[k]])

        J += 1

        K += 1

  Return numlist

threesum的更多相关文章

  1. threeSum问题

    三数之和等于0的问题: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  2. LeetCode: 3Sum

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

  3. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  4. [LeetCode] 3Sum 三数之和

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

  5. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  6. 3sum问题的解决

    其实一开始想错了,把这个问题想难了,导致没有思路,现在好了很多. 题目: Given an array S of n integers, are there elements a, b, c in S ...

  7. 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 un ...

  8. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  9. LeetCode:3Sum, 3Sum Closest, 4Sum

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

随机推荐

  1. Linux IPC实践(13) --System V IPC综合实践

    实践:实现一个先进先出的共享内存shmfifo 使用消息队列即可实现消息的先进先出(FIFO), 但是使用共享内存实现消息的先进先出则更加快速; 我们首先完成C语言版本的shmfifo(基于过程调用) ...

  2. C++11:使用 auto/decltype/result_of使代码可读易维护

    C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...

  3. 《java入门第一季》集合框架引入与面试题

    注:在开始的几篇集合介绍里,不包含泛型的概念.泛型在讲述所有集合后再加入进去. 集合的由来:    我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我 ...

  4. [加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Ext.Net 1.X_读写配置文件

    [摘要] 有N个ERP数据库帐套,需要从XML文件中读取. 加载指定路径的XML /// <summary> /// 取得帐套列表 /// </summary> private ...

  6. Java之美[从菜鸟到高手演变]之设计模式四

    在阅读过程中有任何问题,请及时联系:egg. 邮箱:xtfggef@gmail.com 微博:http://weibo.com/xtfggef 转载请说明出处:http://blog.csdn.net ...

  7. 理解WebKit和Chromium: Chromium插件和扩展基础

    转载请注明原文地址:http://blog.csdn.net/milado_nju ##概述 插件和扩展是一种扩充浏览器功能的技术,在之前我们介绍过NPAPI插件技术,在Chromium中,远远不只是 ...

  8. 用m4 macros创建文本文件

    用m4 macros创建文本文件   原文链接: http://ldp.linux.no/linuxfocus/ChineseGB/September1999/article111.html 补充阅读 ...

  9. 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...

  10. 【编程练习】kmp算法代码

    代码来自: http://blog.csdn.net/v_JULY_v #include "StdAfx.h" #include <iostream> using na ...