LeetCode1两数之和、15三数之和
1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
15.三数之和
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
solution1 暴力破解
//两次循环解两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] re = new int[2];
for (int i = 0; i < nums.length - 1; i ++) {
for (int j = i + 1; j < nums.length; j ++){
if (nums[i] + nums[j] == target) {
re[0] = i;
re[1] = j;
break;
}
}
}
return re;
}
}
//三次循环解三数之和,超出时间限制
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length <= 2) {
return Collections.emptyList();
}
// Set集合,去除重复的数组
Set<List<Integer>> re = new LinkedHashSet<>();
for (int t = 0; t < nums.length - 2; t ++) {
for (int i = t + 1; i < nums.length - 1; i ++){
for (int j = i + 1; j < nums.length; j ++) {
if (nums[t] + nums[i] + nums[j] == 0) {
List<Integer> value = Arrays.asList(nums[t],nums[i],nums[j]);
Collections.sort(value);
re.add(value);
}
}
}
}
return new ArrayList<>(re);
}
}
solution2 两指针
//时间复杂度n^2
//先排序然后遍历数组,整两指针向中夹逼
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length <= 2) {
return Collections.emptyList();
}
Arrays.sort(nums);
List<List<Integer>> res = new LinkedList<>();
for (int k = 0; k < nums.length - 2; k ++) {
if (nums[k] > 0) return res;
if (k > 0 && nums[k] == nums[k-1]) continue;
if(k == 0 || k >= 0 && nums[k] != nums[k - 1]) {
for (int i = k + 1,j = nums.length - 1; i < j;){
int sum = nums[i] + nums[j];
int min = 0 - nums[k];
if (sum == min) {
res.add(Arrays.asList(nums[i],nums[j],nums[k]));
while(i < j && nums[i] == nums[i+1]) i ++;
while(i < j && nums[j] == nums[j-1]) j --;
i ++;
j --;
}
else if (sum < min) {
i ++;
}else j --;
}
}
}
return res;
}
}
哈希表法
//时间复杂度为O(2n)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0;i < nums.length;i++){
map.put(nums[i],i);
}
for (int i = 0;i < nums.length; i ++) {
int diff = target - nums[i];
if (map.containsKey(diff) && map.get(diff) != i){
return new int[]{i,map.get(diff)};
}
}
return null;
}
}
//一次哈希,时间复杂度为o(n)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0;i < nums.length; i ++) {
int diff = target - nums[i];
if (map.containsKey(diff)) {//遍历之前的map里是否包含差
return new int[]{i,map.get(diff)};
}
map.put(nums[i],i);
}
return null;
}
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) return Collections.emptyList();
List<List<Integer>> res = new ArrayList();
Set<Integer> set = new HashSet();
Arrays.sort(nums);
for (int i = 0;i<nums.length;i++){
for (int j = i+1;j < nums.length;j ++) {
int diff = 0-nums[i]-nums[j];
if (set.contains(diff)) {
List<Integer> newAdded = Arrays.asList(diff, nums[j], nums[i]);
if (!res.contains(newAdded)) {
res.add(newAdded);
}
}
}
set.add(nums[i]);
}
return res;
}
}
//两遍遍历加hash
//list 可以用contains()查看是否重复
LeetCode1两数之和、15三数之和的更多相关文章
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- LeetCode:两数之和、三数之和、四数之和
LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 代码随想录算法训练营day07 | leetcode 454.四数相加II 383. 赎金信 15. 三数之和 18. 四数之和
LeetCode 454.四数相加II 分析1.0 这个最直接暴力法,不过过于暴力了,害怕.jpg 失误 读题理解失误:题目要求的是四元组的个数,读完题到我这里成了输出四元组,悲哉 分析2.0 记录有 ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- 【Leetcode】两数之和,三数之和,四数之和
两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- LeetCode 15. 三数之和(3Sum)
题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 【LeetCode】15.三数之和
题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...
- leetcode题目15.三数之和(中等)
题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
随机推荐
- 多源异构数据信息的融合方式0 - Dempster/Shafer 证据理论(D-S证据理论)
Dempster/Shafer 证据理论(D-S证据理论)的大体内容如下: 一.简介: 在理论中,由互不相容的基本命题组成的完备集合Θ称为识别框架,表示对于某一问题的所有可能答案,但是只有一个答案是正 ...
- 可观测性数据收集集大成者 Vector 介绍
如果企业提供 IT 在线服务,那么可观测性能力是必不可少的."可观测性" 这个词近来也越发火爆,不懂 "可观测性" 都不好意思出门了.但是可观测性能力的构建却着 ...
- Noi-Linux 2.0 装机+使用整合
写在前面 网上的东西比较多,也比较杂乱,不是很方便,所以我整合了一些关于 Noi-Linux2.0 虚拟机装机方法+代码编辑环境+实地编程的介绍,看完至少能用起来打代码了. NOI 官网公告(JS 开 ...
- 微软发布开源平台 Radius:高效构建、运行基于Dapr 云原生应用程序
Microsoft Azure 孵化团队很高兴地宣布[1]推出一个名为 Radius 的新开放应用程序平台,该平台将应用程序置于每个开发阶段的中心,重新定义应用程序的构建.管理和理解方式.Radius ...
- CSP 初赛复习
想要做一些不需要思考也算不得摆烂的事,但发现很难找到符合上述要求的学习内容. 突然想到还剩两天就 CSP 初赛了.虽然在 LN 想过不了初赛纯属搞笑,但为了不让自己的分数太难看还是简单复习一下. 没有 ...
- MongoDB 中的锁分析
MongoDB 中的锁 前言 MongoDB 中锁的类型 锁的让渡释放 常见操作使用的锁类型 如果定位 MongoDB 中锁操作 1.查询运行超过20S 的请求 2.批量删除请求大于 20s 的请求 ...
- 使用 Jenkins + Github + dokcer-compose 部署项目-实战篇
使用 Jenkins + Github + dokcer-compose 部署项目-实战篇 需要声明的一点是,此处实现的项目自动构建原理是 Github+Jenkins 的 webhook,因此得保证 ...
- python 安装包时 ERROR: Failed building wheel for webrtcvad
报错信息: error: subprocess-exited-with-error × Building wheel for webrtcvad (pyproject.toml) did not ru ...
- SHA256算法加密工具类
代码如下,请自取 /** * @description: SHA256算法加密 * @author: luolei * @Date: 2022-10-31 17:16 */ public class ...
- Educational Codeforces Round 105 (Rated for Div. 2) A-C题解
写在前边 链接:Educational Codeforces Round 105 (Rated for Div. 2) A. ABC String 链接:A题链接 题目大意: 给定一个有\(A.B.C ...