3Sum and 2Sum
leetcode 15.三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[[-1, 0, 1],[-1, -1, 2]
]
class Solution(object):def threeSum(self, nums):""":type nums: List[int]:rtype: List[List[int]]"""nums_len = len(nums)ls = []if nums_len < 3:return lsnums.sort()for i in range(nums_len - 2):if nums[i] > 0:breakif i == 0 or nums[i] != nums[i - 1]:l = i + 1r = nums_len - 1sum = 0 - nums[i]while l < r:if nums[l] + nums[r] == sum:ls.append((nums[i], nums[l], nums[r]))while l < r and nums[l] == nums[l + 1]:l += 1while l < r and nums[r] == nums[r - 1]:r -= 1l += 1r -= 1elif nums[l] + nums[r] < sum:l += 1else:r -= 1return ls
167.两数之和(有序)
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
思路:
最直接的思考:暴力解法。双层遍历,O(n^2)
暴力解法没有充分利用原数组的性质 - 有序
有序?二分搜索?O(nlogn)
class Solution:def twoSum(self, numbers: List[int], target: int) -> List[int]:#时间复制读O(n)#空间复杂度O(1)i, j = 0, len(numbers)-1while i < j:if numbers[i] + numbers[j] == target:breakelif numbers[i] + numbers[j] < target:i += 1else:j -= 1return [i+1, j+1]
leetcode1. 两数之和(值不重复)
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
思路:由于是不重复的值,考虑使用字典。字典的key存储值,value存储值的下标。遍历列表,如果target-key不在字典里面,那么把当前的key放入字典。否则返回target-key对应的value和当前值对应的下标。
class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:dic = {}for i, j in enumerate(nums):if target - j in dic:return [dic[target - j], i]else:dic[j] = i
3Sum and 2Sum相关推荐
- 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum...
2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode -- 3Sum
Question: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F ...
- [LeetCode]#13 3sum
一.题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 3sum、3Sum closet、 4sum
1. 3Sum [题目] Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0 ...
- 3Sum 3Sum Closest 4Sum
简单延续2sum的做法,用循环,不过代码比较长.原本很多地方写了break,后来发现都不应该加. class Solution { public:vector<vector<int> ...
- 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 923. 3Sum With Multiplicity
题目地址: 做这道题前需要先做:LeetCode 15. 3Sum–Java,Python解法 题目地址:3Sum With Multiplicity - LeetCode Given an inte ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
最新文章
- 自定义input type=file 样式的方法
- 晶体管参数在实际使用中的意义
- 复数特征值求特征向量_深刻地认识特征值
- Atitit.java相比c#.net的优点 优缺点 v2 q330
- SAP TableControl 控制单个单元格输入
- java 下载文件选择下载路径_Java后台如何根据路径地址下载文件?
- 自定义sort函数第三个参数的规则
- c语言如何输入矩阵_如何在 COMSOL 软件中调试外部材料
- Linux-3.2.0.24中内核的Netlink测试使用
- python语言中文怎么读-python中文读什么
- vue权限问题解决方案
- CF932E Team Work
- css居中的几种方法_css两种常用的不定宽高的水平垂直居中方法,记住它,不再为样式发愁...
- js高级学习笔记(b站尚硅谷)-2-数据、变量、内存三者的关系
- 基于点云的骨骼感知三维人体形状重建
- 计算机网络周志500字,计算机网络实习周记.doc
- 金蝶云星空API调用实践
- BZOJ3717: [PA2014]Pakowanie
- 深度学习在美团搜索广告排序的应用实践
- Java项目:小区物业管理系统(java+Springboot+ssm+mysql+maven+jsp)