算法—查找三数相加为零的三元组

package algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组
 * <p>
 * 注意:答案中不可以包含重复的三元组。
 * 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:
 * [
 * [-1, 0, 1],
 * [-1, -1, 2]
 * ]
 * 结论: test()没有去重的情况下,耗时是calculate()的9.4倍,数据越多越明显。
 *
 *
 * @author dingwen
 * 2021.06.18 09:20
 */
public class Algoriehm01 {
    private static int[] nums = {-1, 0, 1, 2, -1, -4, -8, 8, 6, 36, -56, -41, -10, 10, 4, 3, -3, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22,-50,-51,-52,-53,-54,-55,-56,-57,-58,-59,-60,52,53,54,55,56,57,58,59,60};

    public static void main(String[] args) {
        viewTime(true,"calculate");
        viewTime(false,"test");
    }

    public static List<List<Integer>> calculate() {

        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 3) {
            return result;
        }

        // 双轴快排 实现
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int left = 0;
            int right = nums.length - 1;
            while (left < right) {
                int sum = nums[left] + nums[right];
                if (i == left || i == right) {
                    break;
                }
                if ((num + sum) < 0) {
                    left++;
                }
                if ((num + sum) > 0) {
                    right--;
                }
                if ((num + sum) == 0) {
                    result.add(Arrays.asList(nums[left], nums[i], nums[right]));
                    break;
                }
            }
        }

        return result;
    }

    public static List<List<Integer>> test() {
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                for (int k = 0; k < nums.length; k++) {
                    if ((nums[i] + nums[j] + nums[k] == 0)) {
                        //todo 重复值处理
                        result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                    }
                }
            }
        }
        return result;
    }

    public static void viewTime(boolean flag,String title) {
        long start = System.nanoTime();
        if (flag) {
            System.out.println("calculate() = " + calculate());
        } else {
            System.out.println("test() = " + test());
        }
        long end = System.nanoTime();
        System.out.println(title + "耗时 = " + (end - start));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dingwen_blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值