15. 三数之和

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

**注意:**答案中不可以包含重复的三元组

复制代码
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> millionYuanList = new ArrayList<>();
        //人都不够仨,还三胎。。。
        if(nums.length < 3) {
            return millionYuanList;
        }

        Arrays.sort(nums);      //孩子们按个头排好队
        for(int i = 0; i < nums.length; i++) {
            //如果老大都大于0,后面的兄弟肯定都大于0,直接返回就行
            if(nums[i] > 0) break;
            Integer first = nums[i];    //老大出列,站好别动

            //老大想再往后占个位,多领一次奖,这可是不行滴。。。还是回家让妈妈再给生三个小弟弟吧^_^
            if(i > 0 && nums[i] == nums[i - 1]) continue;

            //画个圈,让各家老二在里面呆着
            Set<Integer> set = new HashSet<>();
            for(int j = i + 1; j < nums.length; j++) {
                //老三出列,一会你和老大一块到圈里找老二
                int third = nums[j];
                int second = -(first + third);      //目标是:老大 + 老二 + 老三 = 0
                //找到老二了,记到中奖名单上
                if(set.contains(second)) {
                    millionYuanList.add(new ArrayList<>(Arrays.asList(first, third, -(first + third))));

                    //老三也想多领奖。。。额。。。等会一块回家找妈妈去吧
                    while(j < nums.length - 1 && nums[j] == nums[j + 1]) j++;
                }
                set.add(third);
            }
        }
        return millionYuanList;
    }
}

三层循环

复制代码
public class Solution {

    public static void main(String[] args) {
        int[] nums = new int[]{-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0};
        System.out.println(threeSum(nums));
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> threes = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    if (i == j || j == k || i == k) {
                        continue;
                    }
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        List<Integer> three = new ArrayList<>();
                        three.add(nums[i]);
                        three.add(nums[j]);
                        three.add(nums[k]);
                        if (!contain(three, threes)) {
                            threes.add(three);
                        }
                    }
                }
            }
        }
        return threes;
    }

    public static boolean contain(List<Integer> three, List<List<Integer>> threes) {
        for (List<Integer> list : threes) {
            if (three.contains(list.get(0)) && three.contains(list.get(1)) && three.contains(list.get(2))
                    && list.contains(three.get(0)) && list.contains(three.get(1)) && list.contains(three.get(2))) {
                return true;
            }
        }
        return false;
    }
}
相关推荐
董董灿是个攻城狮1 小时前
5分钟搞懂什么是窗口注意力?
算法
Dann Hiroaki1 小时前
笔记分享: 哈尔滨工业大学CS31002编译原理——02. 语法分析
笔记·算法
qqxhb3 小时前
零基础数据结构与算法——第四章:基础算法-排序(上)
java·数据结构·算法·冒泡·插入·选择
FirstFrost --sy4 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
森焱森4 小时前
垂起固定翼无人机介绍
c语言·单片机·算法·架构·无人机
搂鱼1145145 小时前
(倍增)洛谷 P1613 跑路/P4155 国旗计划
算法
Yingye Zhu(HPXXZYY)5 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
丶小鱼丶6 小时前
排序算法之【归并排序】
java·排序算法
无聊的小坏坏6 小时前
三种方法详解最长回文子串问题
c++·算法·回文串
长路 ㅤ   6 小时前
Java后端技术博客汇总文档
分布式·算法·技术分享·编程学习·java后端