三数之和

题目描述:给你一个包含 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-三数之和的更多相关文章

  1. LeetCode:三数之和【15】

    LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...

  2. [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 ...

  3. 【LeetCode】三数之和【排序,固定一个数,然后双指针寻找另外两个数】

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  4. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  5. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

  6. LeetCode——15. 三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  7. LeetCode 15. 三数之和(3Sum)

    题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  8. [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? ...

  9. [LeetCode]15. 三数之和(数组)(双指针)

    题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三 ...

  10. [LeetCode] 15. 三数之和

    题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...

随机推荐

  1. 学习JAVAWEB第六天

    # 今日内容: 1. JavaScript: 1. ECMAScript: 2. BOM: 3. DOM: 1. 事件 ## DOM简单学习:为了满足案例要求 * 功能:控制html文档的内容 * 获 ...

  2. 关于Linux安装中NAT模式和桥接模式的区别详解(转载)

    1.一般我们在创建一个Linux虚拟机时候,会面临三个网络配置选择: 桥接模式.nat模式.host-only模式(主机模式,这个模式用得少,就不介绍了) 2.NAT模式: 所谓nat模式,就是虚拟系 ...

  3. 「NOI十联测」黑暗

    「NOI十联测」黑暗 \(n\) 个点的无向图,每条边都可能存在,一个图的权值是连通块个数的 \(m\) 次方,求所有可能的图的权值和.(n≤30000,m≤15) 令\(ans[n][m]\)为n个 ...

  4. Java8之Stream常用操作方式

    哈喽!大家好,我是[学无止境小奇],一位热爱分享各种技术的博主! [学无止境小奇]的创作宗旨:每一条命令都亲自执行过,每一行代码都实际运行过,每一种方法都真实实践过,每一篇文章都良心制作过. [学无止 ...

  5. 创建spring boot项目并添加多个模块时,启动报 错误: 找不到或无法加载主类

          最近建个项目发现启动报,找不到或无法加载主类,想想肯定是自己配置出问题了,经过排查确实出问题了,(根pom中的bulid为移到子模块中去导致的),下面演示下正确的创建子模块的步奏 1. 创 ...

  6. HTML元素的隐藏方式

    感谢原文作者:幼儿园中的小小白 原文链接:https://blog.csdn.net/weixin_43846130/article/details/95963426 一.元素的隐藏方式: 1.dis ...

  7. vue element InfiniteScroll 无限滚动 入坑记录

    select_law_by_tag() { this.laws_loading.is_loading = true; this.laws_loading.no_more = false; this.e ...

  8. 学习jsp篇:jsp简单实例之一注册

    编程环境:IDEA,Tomcat ,JavaEE 实例一.注册 1.先在IDEA建一个web工程(不懂的可以在网上搜,一大堆..)ServletTest,在工程目录下的web目录建一个文件夹regis ...

  9. Redis的配置文件redis.conf详解

    Redis的配置文件位于redis的安装目录下,一般不要直接操作出厂设置的配置文件,需要对其进行备份.# Redis的配置文件样例: # Redis configuration file exampl ...

  10. 通过ANT生成MANIFEST.MF中的Class-Path属性

    原文地址:http://reason2003.iteye.com/blog/1627353 之前做一个项目,主程序打包成一个jar文件,因为用到了很多第三方的lib包,所以直接通过java命令运行ja ...