三数之和

题目描述:给你一个包含 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. 【Python爬虫】爬虫利器 requests 库小结

    requests库 Requests 是一个 Python 的 HTTP 客户端库. 支持许多 HTTP 特性,可以非常方便地进行网页请求.网页分析和处理网页资源,拥有许多强大的功能. 本文主要介绍 ...

  2. 精通 TensorFlow 1.x·翻译完成

    原文:Mastering TensorFlow 1.x 协议:CC BY-NC-SA 4.0 不要担心自己的形象,只关心如何实现目标.--<原则>,生活原则 2.3.c 在线阅读 Apac ...

  3. Spring学习七:ComponentScan注解

    今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1.ComponentScan注解是什么 其实很 ...

  4. 广播接收者案例_sd卡状态监听

    (1)定义广播接收者 import android.content.BroadcastReceiver; import android.content.Context; import android. ...

  5. mysql查询奇数行或者偶数行数据

    select * from (select @rownum := @rownum+1 as row_num, t.* from 表名 t,(select @rownum:=0) tmp_table o ...

  6. iOS 学习资料Blog 技术论坛等,不断添加中。。。。

    iOS 学习资料整理 http://www.jianshu.com/p/dc81698a873c    中文 iOS/Mac 开发博客列表  https://github.com/tangqiaobo ...

  7. jenkins插件Publish Over SSH因安全问题下架

    最近用docker新搭建了一个jenkins,安装插件的时候发现publish over ssh找不到了,官方给出的解释是存在安全隐患于2022.01.12暂停分发,官方解释如下:https://ww ...

  8. 解决/WEB-INF目录下的jsp页面引入webRoot下的Js、css和图片的问题

    通常把jsp页面放在webRoot的/WEB-INF下可以防止访问者直接输入页面. 而webRoot的/WEB-INF下的页面是受保护的,用户无法通过形如http://localhost:8080/t ...

  9. python使用泛型

    所谓的泛型, 就是将数据类型作为参数进行传递, 即在我们用的时候确定数据类型, 这是一种在面向对象语言中经常使用的特性 一般类使用 以SQLAlchemy举例 比如: 我们统一写个将数据保存到数据库的 ...

  10. C# 实例解释面向对象编程中的开闭原则

    在面向对象编程中,SOLID 是五个设计原则的首字母缩写,旨在使软件设计更易于理解.灵活和可维护.这些原则是由美国软件工程师和讲师罗伯特·C·马丁(Robert Cecil Martin)提出的许多原 ...