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]
]
题目解析:
首先对数组进行排序(时间复杂度O(nlog(n))),排序后固定一个数 nums[i],再使用左右指针指向 nums[i]后面的两端,数字分别为 nums[L] 和 nums[R],计算三个数的和 sum 判断是否满足为 0,满足则添加进结果集
如果 nums[i]大于 0,则三数之和必然无法等于 0,结束循环
如果 nums[i] == nums[i-1],则说明该数字重复,会导致结果重复,所以应该跳过
当 sum == 0时,nums[L] == nums[L+1] 则会导致结果重复,应该跳过,L++
当 sum0 时,nums[R] = nums[R-1] 则会导致结果重复,应该跳过,R--
时间复杂度:O(n^2),n 为数组长度
代码实现:
package com.company; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class Main { public static void main(String[] args) {
int[] nums = new int[]{-1, 0, 1, 2, -1, -4};
System.out.println(threeSum(nums));
} private static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new ArrayList<>();
if (nums == null || nums.length < 3) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
break;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int rigth = nums.length - 1;
while (left < rigth) {
int sum = nums[left] + nums[rigth] + nums[i];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[left], nums[rigth]));
while (left < rigth && nums[left] == nums[left + 1]) {
left++;
}
while (left < rigth && nums[rigth] == nums[rigth - 1]) {
rigth--;
}
left++;
rigth--;
} else if (sum < 0) {
left++;
} else {
rigth--;
}
} }
return result;
}
}
时间复杂度:O(n^2),n 为数组长度
空间复杂度:O(1),常数空间存储
leetcode题目15.三数之和(中等)的更多相关文章
- 【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 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- 力扣(LeetCode)15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 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 < ...
- LeetCode 15. 三数之和(3Sum)
题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
随机推荐
- O057、Delete Volume 操作
参考https://www.cnblogs.com/CloudMan6/p/5648665.html 状态为Available 的volume 才能够被delete,如果volume当前已经被at ...
- sass之mixin的全局引入(vue3.0)
sass之mixin的全局引入(vue3.0) 1.scss文件(mixin.scss) /* 渐变 */ @mixin gradual($color, $color1){ background: $ ...
- httpclient 多附件上传
多附件上传实例: /** * 多附件上传 * @param host * @param uri * @param attachment 附件 * @param param body参数 * @retu ...
- 微信小程序富文本
<div class="weui-panel__bd pad-all fs13 " > <rich-text nodes="{{detail.conte ...
- git托管代码
安装git 在github上创建仓库 搞钥匙 在vscode上托管 初次在不同电脑上上传代码报错 解决:git pull --rebase origin master 链接:https://blog. ...
- 解决 Grep 的多次管道过滤问题
解决 Grep 的多次管道过滤问题 这是个问题,解决了 tail -f crazy.log | grep --line-buffered Hello | grep Time 解决 Grep 的多次管道 ...
- DA_05_Linux(CentOS6.7) 安装MySql5.7数据库
1系统约定 安装文件下载目录:/data/software Mysql目录安装位置:/usr/local/mysql 数据库保存位置:/data/mysql 日志保存位置:/data/log/mysq ...
- Elasticsearch索引操作
一.索引初始化操作 插件推荐使用head.marvel (收费) 1.1 创建新索引 curl -XPUT 'http://localhost:9200/test' -d ' { "sett ...
- Java语言基础(7)
1 for循环 案例:Demo1 1+1/2+1/3+1/4+1/5+1/6+...+1/100 = ? 1/1+1/2+1/3+1/4+1/5+1/6+...+1/100 = ? 分子都是1,分母是 ...
- VS2008配合SQLite开发WINCE、PDA智能设备项目环境搭设。
1.安装vs2008 ---------------------------vs2008上安装TFS步骤(详细请见——http://www.cnblogs.com/mayt/archive/2013/ ...