牛客网:NC54 三数之和

该博客主要探讨了如何优化三数之和问题的解决方案,通过使用Java编程语言实现。作者首先展示了三层循环的原始解法,但由于时间复杂度过高,这种方法无法有效执行。接着,作者提出了一种双指针法,通过排序数组并遍历,减少了时间复杂度,提高了算法效率。这种方法避免了重复值,并在找到满足条件的三数组合后进行去重,以确保结果的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



import java.util.*;

/**
 * @author xienl
 * @description 三数之和
 * @date 2022/6/30
 */

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] num = {0 , 0, 0 , 0};
        System.out.println(solution.threeSum(num));
    }

    /**
     * 三层循环,不行,复杂度过大,执行不完
     * @param num
     * @return
     */
    public ArrayList<ArrayList<Integer>> threeSum2(int[] num) {
        Arrays.sort(num);
        Set<ArrayList<Integer>> res = new HashSet<>();
        for (int i = 0; i < num.length; i++){
            for (int j = i + 1; j < num.length; j++){
                for (int k = j + 1; k < num.length; k++){
                    if (num[i] + num[j] + num[k] == 0) {
                        ArrayList<Integer> temp = new ArrayList<>();
                        temp.add(num[i]);
                        temp.add(num[j]);
                        temp.add(num[k]);
                        Collections.sort(temp);
                        res.add(temp);
                    }
                }
            }
        }
        return new ArrayList<>(res);
    }

    /**
     * 双指针
     *      如果第一个值为a 要想结果为0 ,必须要 第二个和第三个值相加为-a
     * @param num
     * @return
     */
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        if (num.length < 3){
            return new ArrayList<>();
        }
        Arrays.sort(num);
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        int n = num.length;
        for (int i = 0; i < n - 2; i++){
            if (i != 0 && num[i] == num[i - 1]) {
                continue;
            }
            if (num[i] > 0){
                break;
            }
            int target = -num[i];
            int left = i + 1;
            int right = n - 1;
            while (left < right){
                // 两数字和
                int sum = num[left] + num[right];
                if (sum == target){
                    ArrayList<Integer> temp = new ArrayList<>();
                    temp.add(num[i]);
                    temp.add(num[left]);
                    temp.add(num[right]);
                    res.add(temp);

                    // 去重
                    while (left + 1 < right && num[left] == num[left + 1]){
                        left++;
                    }
                    while (right - 1 > left && num[right] == num[right - 1]){
                        right--;
                    }
                    left++;
                    right--;
                } else if (sum > target){
                    right--;
                } else {
                    left++;
                }
            }
        }
        return res;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值