力扣 ——3Sum python (三数之和)实现
题目描述:
中文:
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
英文:
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.
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
if len(nums) < 3:
return result
nums.sort()
for i in range(0, len(nums)):
j = i + 1
k = len(nums) - 1
if i > 0 and nums[i] == nums[i - 1]:
continue #
while j < k:
sum = nums[i] + nums[j] + nums[k]
if sum == 0:
result.append((nums[i], nums[j], nums[k]))
k -= 1
j += 1
while(nums[j] == nums[j - 1] and nums[k] == nums[k + 1] and j < k):
j += 1
elif sum > 0:
k -= 1
while(nums[k] == nums[k + 1] and j < k):
k -= 1
else:
j += 1
while(nums[j] == nums[j - 1] and j < k):
j += 1
return list(set(result))
题目来源:力扣
力扣 ——3Sum python (三数之和)实现的更多相关文章
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- python三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 力扣 ——4Sum (四数之和)python 实现
题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...
- 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]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- 15. 3Sum[M]三数之和
题目 Given an array nums of n integers, are three elements a, b, c in nums such that a+b+c=0? Find all ...
- 3sum 求三数之和等于0,不允许重复
https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...
- 259 [LeetCode] 3Sum Smaller 三数之和较小值
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
随机推荐
- python实战-有道翻译
#导入urllib包里的request请求模块import urllib.request#导入urllib包里的解析模块 import urllib.parse import json content ...
- Es学习第七课, term、terms、match等基本查询语法
term.terms查询 term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword.numeric.date等明确值的 term:查询某个字段里含有某个 ...
- python request(HttpRequest对象)请求的属性、方法笔记
1.属性 path:表示提交请求页面完整地址的字符串,不包括域名,如"/music/bands/the_beatles/". method:表示提交请求使用的HTTP方法.(GET ...
- 终极解决方案: Invalid character found in the request target.
终极解决方案:(导出可能出现) 我的tomcat版本是8.5.32,导出时遇到以下报错. 报错日志: Invalid character found in the request target. Th ...
- Linux系统下安装JDK及环境配置
第一种属于傻瓜式安装,一键安装即可(yum安装): 第二种手动安装,需要自己去Oracle官网下载需要的jdk版本(需官网注册登录才可以下载),然后解压并配置环境. 一.yum一键安装1.首先执行以下 ...
- springBoot03- springboot+jpa+thymeleaf增删改查
参考http://www.mooooc.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 数据库: CREATE TABLE ...
- Java序列化及反序列化
序列化概念: 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列的过程:而Java反序列化是指把字节序列恢复为Java对象的过程. 2.为什么需要序列化与反序列化 我们知道 ...
- JS中的getter和setter
对象有两种属性:(1)数据属性,就是我们经常使用的属性(2)访问器属性,也称存取器属性 存取器属性就是一组获取和设置值的函数.getter负责获取值,它不带任何参数.setter负责设置值,在它的函数 ...
- logstash配置文件详解
logstash pipeline 包含两个必须的元素:input和output,和一个可选元素:filter. 从input读取事件源,(经过filter解析和处理之后),从output输出到目标 ...
- 点击按钮后URL呗改变
这里留个坑,Button默认类型是submit.没有写类型的,可能会导致触发Url改变.要么写类型,要么在按钮对应的Js方法里return.