牛客网:NC54 三数之和



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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值