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 ...
随机推荐
- mvvm与mvc的定义与区别
mvvm: 即Model-View-ViewModel(模型-视图-视图模型)的简写. 模型(Model):后端传递的数据 视图(View):即前端渲染的页面 视图模型:是 mvvm 的核心,是连接 ...
- mybatis 原生写法创建项目
一.创建全局文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuratio ...
- Spring源码-IOC部分-容器初始化过程【2】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- Spring源码-IOC部分-Bean实例化过程【5】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- 看一遍就懂:MVCC原理详解
MVCC实现原理也是一道非常高频的面试题,自己在整理这篇文章的时候,感觉到网上的资料在讲这块知识点上写的五花八门,好像大家的理解并没有一致. 这里将自己所理解的做一个总结,个人会觉得这是一篇含金量挺高 ...
- 标签显示模式(display)
非洲黑人: 皮肤内黑色素含量高,以吸收阳光中的紫外线,保护皮肤内部结构免遭损害,头发象羊毛一样卷曲,使每根卷发周围都有许多空隙,空隙充满空气,卷发有隔热作用. 欧洲白人: 生活寒带或着是说常年温度较低 ...
- Java中线程的状态及其转化
线程状态转化图: 说明: 线程总共包括以下5种状态. 1.新状态New:该状态也叫新建状态,当线程对象被创建后,线程就进入了新建状态.例如:Thread thread = new Thread();. ...
- Java线程--Atomic原子类使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11871241.html Java线程--Atomic原子类使用 package concurr ...
- Linux curl命令进行网络请求
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11841353.html 1. curl get请求: curl http://www.baid ...
- android中listView下拉刷新
Android的ListView是应用最广的一个组件,功能强大,扩展性灵活(不局限于ListView本身一个类),前面的文章有介绍分组,拖拽,3D立体,游标,圆角,而今天我们要介绍的是另外一个扩展Li ...