三数之和

题目描述:给你一个包含 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. JavaScript之 函数节流(Throttling) 与 防抖(Debounce)

    Throttling:在监听鼠标移动事件.盒子滚动事件等使用节流技术能节省性能消耗 /** * 节流函数(每隔t执行一次) */ function Throttling(fn, t) { const ...

  2. vue之Better-Scroll组件 将滚动条滚到最底部

    首先我们需要使用scrollTo这个方法: scrollTo(x, y, time, easing) 参数: {Number} x 横轴坐标(单位 px) {Number} y 纵轴坐标(单位 px) ...

  3. dealloc方法

    dealloc方法基本概念 当一个对象的引用计数器值为0时,这个对象即将被销毁,其占用的内存被系统回收 对象即将被销毁时系统会自动给对象发送一条dealloc消息 (因此, 从dealloc方法有没有 ...

  4. Static块和类加载顺序

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11451040.html  版本:Java8 直接上代码: public class Stati ...

  5. 有序取出Map集合的元素

    最近写到一个程序,返回了map,但是经过查阅资料,map是没有顺序的,各种查阅资料无果,最后自己写了这个方法.. 1,通过map集合的keySet()方法,获取到一个包含map所有key的Set集合 ...

  6. java+selenium脚本编写规范

    2.    源文件规范 2.1    文件名 源文件以最顶层的类名来命名,大小写敏感,文件扩展名为.java 2.2    文件编码 UTF-8 源文件要求编码格式为UTF-8 2.3    源文件结 ...

  7. Aselenium前言

    https://seleniumhq.github.io/docs/index.html https://www.seleniumhq.org/ THE SELENIUM BROWSER AUTOMA ...

  8. Solution -「Ynoi 2018」「洛谷 P4117」五彩斑斓的世界

    \(\mathcal{Description}\)   Link.   给定序列 \(\{a_n\}\),处理 \(m\) 次操作: 给定 \(l,r,x\),把 \([l,r]\) 内所有 \(&g ...

  9. SonarQube之采购选型参考

    SonarQube是DevOps实践中主流的一款质量内建工具,过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如pmd-cpd.checkstyle.findbugs ...

  10. kaptcha验证码参数设置

    Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and optio ...