LeetCode-015-三数之和
三数之和
题目描述:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例说明请见LeetCode官网。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:暴力破解法
首先将数组nums排序,然后通过三重遍历,获取符合条件三元组,中间需要把重复的通过判断过滤掉。
解法二:双指针法
描述首先也是将nums排序,然后第一层遍历和第一种方法一样,获取第一个数字的索引first,然后第二个数字索引second和第三个数字索引third分别从first之后的数组中,从左至右获取符合条件的数字,直到second不小于third为止。
第二种方法比第一种方法少一层循环,所以效率高得多。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
/**
* 方法一:暴力破解法
*
* @param nums
* @return
*/
public static List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
int count = 0;
// 第一层循环:遍历第一个数字
for (int first = 0; first < nums.length - 2; first++) {
if (nums[first] > 0) {
return result;
}
// 不允许重复
if (count > 0 && nums[first] == result.get(count - 1).get(0)) {
continue;
}
// 第二重循环:遍历第二个数字
for (int second = first + 1; second < nums.length - 1; second++) {
// 当前2个数字之和已经大于0了,遍历第三个数字已经没有意义了
if (nums[first] + nums[second] > 0) {
break;
}
// 第三重循环:遍历第三个数字
for (int three = second + 1; three < nums.length; three++) {
if (nums[first] + nums[second] + nums[three] == 0) {
if (count > 0 && nums[first] == result.get(count - 1).get(0) &&
nums[second] == result.get(count - 1).get(1) &&
nums[three] == result.get(count - 1).get(2)) {
continue;
}
ArrayList<Integer> temp = new ArrayList<>();
temp.add(nums[first]);
temp.add(nums[second]);
temp.add(nums[three]);
result.add(temp);
count++;
break;
}
}
}
}
return result;
}
/**
* 方法二:双指针法
*
* @param nums
* @return
*/
public static List<List<Integer>> threeSum1(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
// 第一层循环:遍历第一个数字
for (int first = 0; first < nums.length - 2; first++) {
// 不允许重复
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
int third = nums.length - 1;
for (int second = first + 1; second < nums.length - 1; second++) {
// 不允许重复
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
// second和third 2个从两边一起向中间遍历,这样就减少了一层循环
while (second < third && nums[first] + nums[second] + nums[third] > 0) {
// 当3个数之和大于0时,将third往左移取更小的数,直到 second >= third
third--;
}
// 不存在符合条件的元组
if (second == third) {
break;
}
if (nums[first] + nums[second] + nums[third] == 0) {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(nums[first]);
temp.add(nums[second]);
temp.add(nums[third]);
result.add(temp);
}
}
}
return result;
}
public static void main(String[] args) {
int[] nums = new int[]{-1, 0, 1, 2, -1, -4, -2, -3, 3, 0, 4};
List<List<Integer>> lists = threeSum(nums);
for (List<Integer> list : lists) {
for (Integer integer : list) {
System.out.print(integer + "/");
}
System.out.println();
}
System.out.println();
List<List<Integer>> lists2 = threeSum1(nums);
for (List<Integer> list : lists2) {
for (Integer integer : list) {
System.out.print(integer + "/");
}
System.out.println();
}
}
}
LeetCode-015-三数之和的更多相关文章
- LeetCode:三数之和【15】
LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...
- [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】三数之和【排序,固定一个数,然后双指针寻找另外两个数】
给定一个包含 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 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 15. 三数之和(3Sum)
题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- [Leetcode 15]三数之和 3 Sum
[题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...
- [LeetCode]15. 三数之和(数组)(双指针)
题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三 ...
- [LeetCode] 15. 三数之和
题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...
随机推荐
- Java中的增强型for循环
下面是关于增强型for循环对一维数组与二维数组遍历的具体实现: public class NewForLoop { public static void main(String[] args) { i ...
- 学习:二维码、QR码、J4L-QRCode、java
开源码 Java 解码器(编码解码)下载:http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip Java QR Code O ...
- 列出ubuntu软件管理工具apt的一些用法(自由总结)
安装软件包 [root@CentOS7 ~]#apt install tree 删除软件包 [root@CentOS7 ~]# apt remove tree 列出仓库软件包 [root@CentOS ...
- 06.python语法入门--与用户交互、运算符
与用户交互 输入 input # python2与python3的区别 '''python3''' # 将获取到的用户输入赋值给变量名res res = input(' ...
- 基于zynq XC7Z100 FMC接口通用计算平台
1.板卡概述 此板卡是北京太速研发,由SoC XC7Z100-2FFG900I芯片来完成卡主控及数字信号处理,XC7Z100内部集成了两个ARM Cortex-A9核和一个kintex 7的FPGA, ...
- Solution -「ACM-ICPC BJ 2002」「POJ 1322」Chocolate
\(\mathcal{Description}\) Link. \(c\) 种口味的的巧克力,每种个数无限.每次取出一个,取 \(n\) 次,求恰有 \(m\) 个口味出现奇数次的概率. \( ...
- Solution -「ZJOI 2019」「洛谷 P5326」开关
\(\mathcal{Description}\) Link. 有 \(n\) 个开关,初始时所有开关的状态为 \(0\).给定开关的目标状态 \(s_1,s_2,\cdots,s_n\).每 ...
- pypi服务器搭建
pypi服务器搭建 [root@localhost ~]# hostnamectl set-hostname --static pypi-server [root@pypi-server ~]# yu ...
- Spring MVC参数绑定(如何接收请求参数及返回参数)
在SpringMVC interceptor案例实践中遇到了获取jsp表单传递参数失败的问题,怎么的解决的呢?下面详细介绍. 先讲述下https://www.cnblogs.com/ilovebath ...
- 入职钉钉接近半年,谈谈自身的新人landing体会
一.适应新环境 到了一个新环境里,身边的一切都比较陌生,与同事和领导都不太熟,一开始说话说不到一起都比较正常.在和同事吃饭的时候,多听多参与聊天,主动一些,逐渐熟悉起来就好了. 另外,我的工作台bas ...