美文网首页程序员
LeetCode-数组-三数之和

LeetCode-数组-三数之和

作者: 断风雨_2669 | 来源:发表于2019-02-26 13:37 被阅读3次

题目描述

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。 习题原文

解题思路

求三数之和为 0, nums[i] + nums[j] + nums[k] = 0 可以转换为求两个数之和,也即是 nums[i] + nums[j] = -nums[k]; 那么就可以通过指针移动去查找满足公式的数字,而为了方便控制指针的移动,前提是一组数字是有序的。

那么大概的解题思路如下:

  • 对数组排序
  • 定义三个指针 i, left, right; 指针 i 指向的是求和项,指针 left, right 分别指向数组的头部和尾部
  • 当 -nums[i] = nums[left] + nums[right] 时满足公式,指针 left, right 继续向中部移动
  • 当 -nums[i] > nums[left] + nums[right] 时指针 left 向中部移动
  • 当 -nums[i] < nums[left] + nums[right] 时指针 righr 向中部移动
  • 当指针 left,right 交叉时停止移动,指针 i 向下移动,指针 left, right 重置

其流程如下图所示:

image

实现

public static List<List<Integer>> solution(int[] nums) {
    // 排序
    Arrays.sort(nums);

    List<List<Integer>> rs = new ArrayList<>();

    int i = 0, left = 1, right = nums.length - 1;

    while (i < nums.length - 2) {
        while (true) {
            // 左右指针交叉退出
            if (right <= left) {
                break;
            }

            if (-nums[i] == nums[left] + nums[right]) {
                rs.add(Arrays.asList(nums[i], nums[left], nums[right]));

                // 过滤同一结果内重复的数字
                // 左右指针继续向中间移动
                while (true) {
                    if (left >= nums.length - 1) {
                        break;
                    }

                    if (nums[left] != nums[left + 1]) {
                        left++;
                        break;
                    } else {
                        left++;
                    }
                }

                while (true) {
                    if (right <= 0) {
                        break;
                    }

                    if (nums[right] != nums[right - 1]) {
                        right--;
                        break;
                    } else {
                        right--;
                    }
                }

                continue;
            }

            if (-nums[i] <= nums[left] + nums[right]) {
                right--;
                continue;
            }

            if (-nums[i] > nums[left] + nums[right]) {
                left++;
                continue;
            }
        }

        // 过滤同一结果值的数字
        while (true) {
            if (i >= nums.length - 1) {
                break;
            }

            if (nums[i] != nums[i + 1]) {
                i++;
                break;
            } else {
                i++;
            }
        }

        //i++;
        left = i + 1;
        right = nums.length - 1;
    }

    return rs;
}

相关文章

  • 【leetcode-数组】三数之和

    【leetcode-数组】三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 ...

  • LeetCode-数组-三数之和

    题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a +...

  • leetcode-三数之和

    这道题用了双指针法。 首先对数组进行排序,排序后固定一个数 nums[i],再使用左右指针指向 nums[i]后面...

  • LeetCode 第18题:四数之和

    1、前言 2、思路 采用三数之和的思路,原本三数之和可以分解为:数组中的一个数 + 此数右边的数求两数之和,那么四...

  • 算法精选题总结之数组类

    1.判断数组中三数之和2.判断数组中最接近的三数之和3.排序数组中剔除重复的数4.数组序列字典序下一个排列5.旋转...

  • leetcode top100

    1.求两数之和(数组无序) 2.求电话号码的字母组合 3.三数之和 4.两数之和(链表)

  • 双指针总结

    左右指针 主要解决数组中的问题:如二分查找 盛最多水的容器 三数之和 四数之和 最接近三数之和 快慢指针 主要解决...

  • 数组--三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + ...

  • LintCode三数之和系列问题

    三数之和 LintCode57 三数之和解题思路:先对数组排序,然后开始遍历,对于数组中的每一个元素,用两指针往中...

  • 两数之和(golang)

    原题:两数之和 关联:两数之和 II - 输入有序数组(golang)两数之和 IV - 输入 BST(golang)

网友评论

    本文标题:LeetCode-数组-三数之和

    本文链接:https://www.haomeiwen.com/subject/nijayqtx.html