算法题

问题描述:
在一些给定的数中,找到三个数,他们相加的和是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. LCD正向扫描和反向扫描

    LCD正向扫描和反向扫描 LCD扫描一般分正向扫面和反向扫描,分别针对正装和倒装结构(如下): 有时候提到长边扫描和短边扫描应该是针对横屏和竖屏的设置,大部分显示屏是正向扫描,是否都支持,和玻璃有关, ...

  2. 在IFrame中查找IFRAME中的元素的方式

    下面是内部iframe找外部mainFrame的情况  var websiteSearchButton = window.parent.parent.document.getElementById(' ...

  3. 类模板语法知识体系梳理(包含大量常犯错误demo,尤其滥用友元函数的错误)

    demo 1 #include <iostream> #include <cstdio> using namespace std; //template <typenam ...

  4. CTBS问题百科

    1.浏览CTBS网站时,Service Unavailable或应用程序池自动停止的现象 解决方法: 点击"开始"-"控制面板"-"管理工具" ...

  5. 反对网抄,没有规则可以创建目标"install" 靠谱解答

    在ubuntu下遇到这个问题,原因其实很简单,你不能用WINDWOS下的方法用图形方式打开,然后点了一下按扭"解压缩",生成了一个文件夹. 的确,这个文件夹看起来和正常的没有什么区 ...

  6. mysql进阶(十八)完全卸载mysql数据库图文教程

    完全卸载mysql数据库图文教程 有时候MySQL不能完全卸载,这时候必须通过一些途径删除掉注册表和一些残余的文件,然后才能重新安装才可以成功! 方法/步骤 1.控制面板-->所有控制面板项-- ...

  7. MT6575 充电流程

    1,目前充电主要包括Power Off Charging(关机充电) .IPO Charging(休眠充电)和 OS Charging(开机充电) 三个部分 2,Power Off Charging ...

  8. STL - priority_queue(优先队列)

    优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...

  9. Android电话拦截实现以及TelephonyManager监听的取消

    由于毕业设计题目涉及到电话拦截这一块.所以鼓捣了一下.遇到了一些问题,总结一下,以免忘记,也希望能帮助其他新人们. 本篇博客目的:实现电话的拦截 会遇到的问题:android studio下AIDL的 ...

  10. 【一天一道LeetCode】#328 Odd Even Linked List

    一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...