【LeetCode每天一题】3Sum(三数之和)
Given an array nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
思路
我们先对数组进行排序,然后从第一个数字开始查找, 然后在后面的数组中查找是否有满足条件的(使用两个指针一个指向开头,一个指向尾部开始遍历)。时间复杂度为O(n*n), 空间复杂度为O(1)。
图示步骤
解决代码
- class Solution(object):
- def threeSum(self, nums):
- nums.sort()
- length = len(nums)
- res_list = []
- for i in range(length - ): # 以此下标的数字和其他进行匹配
- if i > and nums[i] == nums[i - ]: # 如果和上一个数字相等,直接返回。 因为上一次已经查找过
- continue
- start, end = i + , length-1 # 在后面的数组中设置起始指针和尾部指针
- while start < end:
- tem = nums[i] + nums[start] + nums[end]
- if tem == : # 如果三个数字之和等于0,将其进行添加
- res_list.append([nums[i], nums[start], nums[end]])
- while start < end and nums[start] == nums[start+]: # 判断start下一个数字和当前数字是否相等,相等下移一位。不然会添加进重复的元素
- start += 1
- while start < end and nums[end] == nums[end-]: # 判断end上一个数字和当前数字是否相等,相等则跳过。
- end -=
- start +=
- end -=
- elif tem < : # 和小于 0 的话,start进位
- start +=
- else: # 和大于0的话,end减一
- end -=
- return res_list
【LeetCode每天一题】3Sum(三数之和)的更多相关文章
- leetcode第15题:三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- [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 ...
- [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 ...
- LeetCode(15):三数之和
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- [leetcode]15. 3Sum三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leecode第十五题(三数之和)
class Solution { public: void quick_order(vector<int>& num, int star, int en)//快排 { int st ...
随机推荐
- MySQL主从同步技术
一,先装mysql 准备两台服务器,主服务器是10.0.0.200 副服务器是10.0.0.128主服务器当作mysql的服务端,下载mysql-serveryum install mysql-ser ...
- js 基础知识总结
1.switch结构 switch语句部分和case语句部分,都可以使用表达式. switch(1 + 3) { case 2 + 2: f(); break; default: neverhappe ...
- [No000012A]WPF(2/7):布局,容器和布局转换[译]
概要 在上一篇文章中,我讨论了一些WPF应用的基础知识,它们是使用WPF的基本架构和内部结构.在本文中,我将讨论编写你的第一个WPF程序的基本的东西和怎么在你的窗口中布局控件.这是每一个想使用WPF的 ...
- Javascript:一些基本语法
便于日后复习快速回忆起来,把Javascript一些没那么普遍的语法特性写一写. Javascript作为三剑客的灵魂,我把它写在body里 <!DOCTYPE html> <htm ...
- linux添加新硬盘
1.添加新磁盘 2.fdisk -l查看磁盘被识别的名称 3.如果输入fdisk -l命令没有找到新的磁盘,按下面步骤操作 1)进入到cd /sys/class/scsi_host/ 2)echo & ...
- block diagonal matrix 直和 块对角矩阵 不完美 有缺陷 缩放 射影几何
小结: 1.block diagonal matrix 直和 块对角矩阵 A block diagonal matrix is a block matrix that is a square mat ...
- [daily][dpdk] 网卡offload识别包类型;如何模拟环境构造一个vlan包
第一部分 硬件识别包类型 网卡,是可以识别包类型的.在dpdk的API中.识别完之后,存在这个结构里: struct rte_mbuf { ...... union { uint32_t packet ...
- 搭建Kubernetes服务集群遇到的问题
kube-proxy问题: Apr 12 09:42:49 compute1 kube-proxy[12965]: E0412 09:42:49.602342 12965 reflector.go:2 ...
- jquery基础学习之事件篇(三)
一.鼠标事件 click 单击 与 dbclick 双击 用于监听用户的点击操作,在同一元素上同时绑定 click 和 dblclick 事件是不可取的...根据浏览器支持不同一个点击事件是由mous ...
- Appium入门(9)—— Appium API
摘自:http://www.testclass.net/appium/appium-base-api-01/ 1.安装: installApp() driver.installApp("d: ...