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;
    }
}
相关推荐
股朋公式网2 小时前
斩仙飞刀、 通达信飞刀 源码
python·算法
不吃橘子的橘猫2 小时前
NVIDIA DLI 《Build a Deep Research Agent》学习笔记
开发语言·数据库·笔记·python·学习·算法·ai
Xの哲學2 小时前
Linux CFS 调度器深度解析
linux·服务器·算法·架构·边缘计算
bedynamic2 小时前
蚁群算法原理及实现
算法·智能算法
Coovally AI模型快速验证2 小时前
当小龙虾算法遇上YOLO:如何提升太阳能电池缺陷检测精度?
人工智能·深度学习·算法·yolo·目标检测·无人机
surtr13 小时前
常见排序模板(冒泡排序,希尔排序,堆排序,归并排序,快速排序)
数据结构·算法·贪心算法·排序算法
fengfuyao9853 小时前
经典MUSIC算法程序以及测角精度与阵元间距、阵元数、信噪比、快拍数等的关系
算法
十八岁讨厌编程3 小时前
【算法训练营 · 补充】LeetCode Hot100(下)
算法·leetcode·职场和发展
一路往蓝-Anbo3 小时前
C语言从句柄到对象 (三) —— 抛弃 Malloc:静态对象池与索引句柄的终极形态
c语言·开发语言·数据结构·stm32·单片机·算法
fantasy_arch4 小时前
SVT-AV1 B帧决策和mini-GOP决策分析
算法·av1