三数之和 Leecode 2024/12/02

思路:1. 找到所有不重复下标的整数组合。2. 在所有组合中找到和为0的组。

暴力解法超时!!!

复制代码
public static List<List<Integer>> threeSum(int[] nums) {
    int num_len = nums.length;
    HashSet<List<Integer>> rs = new HashSet<>();
    for (int i = 0; i < num_len-2; i++){
        for (int j = i+1; j < num_len-1; j++){
            for (int k = j+1; k < num_len; k++){
                if (nums[i] + nums[j] + nums[k] == 0){
                    // 将 HashSet 转换为 List
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[j]);
                    list.add(nums[k]);
                    list.sort(null);
                    rs.add(list);
                }
            }
        }
    }
    List<List<Integer>> result = new ArrayList<>(rs);
    return result;
}

排序 + 双指针

先将 nums 排序,时间复杂度为 O(NlogN)。

固定 3 个指针中最左(最小)元素的指针 k,双指针 i,j 分设在数组索引 (k,len(nums)) 两端。

双指针 i , j 交替向中间移动,记录对于每个固定指针 k 的所有满足 nums[k] + nums[i] + nums[j] == 0 的 i,j 组合:

当 nums[k] > 0 时直接break跳出:因为 nums[j] >= nums[i] >= nums[k] > 0,即 3 个元素都大于 0 ,在此固定指针 k 之后不可能再找到结果了。

当 k > 0且nums[k] == nums[k - 1]时即跳过此元素nums[k]:因为已经将 nums[k - 1] 的所有组合加入到结果中,本次双指针搜索只会得到重复组合。

i,j 分设在数组索引 (k,len(nums)) 两端,当i < j时循环计算s = nums[k] + nums[i] + nums[j],并按照以下规则执行双指针移动:

当s < 0时,i += 1并跳过所有重复的nums[i];

当s > 0时,j -= 1并跳过所有重复的nums[j];

当s == 0时,记录组合[k, i, j]至res,执行i += 1和j -= 1并跳过所有重复的nums[i]和nums[j],防止记录到重复组合。

复制代码
public static List<List<Integer>> threeSum(int[] nums) {
    // 1. 排序
    Arrays.sort(nums);
    int num_len = nums.length;

    List<List<Integer>> result = new ArrayList<>();
    // 2. 双指针遍历

    for (int i = 0; i < num_len - 2; i++) {
        int j = i+1, k = num_len - 1;
        if (i > 0 && nums[i] == nums[i-1]) continue; // 本次双指针搜索只会得到重复组合
        if (nums[i] > 0) continue; //在此固定指针 i 之后不可能再找到结果了
        while(j < k){
            int s = nums[i] + nums[j] + nums[k];
            if (s == 0) {
                List<Integer> temp = new ArrayList<>();
                temp.add(nums[i]);
                temp.add(nums[j]);
                temp.add(nums[k]);
                result.add(temp);
                // 跳过重复的元素
                while (j < k && nums[j] == nums[j + 1]) j++;
                while (j < k && nums[k] == nums[k - 1]) k--;
                k--;
                j++;
            } else if (s > 0) {
                k--;
            } else {
                j++;
            }
        }
    }
    return result;
}
相关推荐
JicasdC123asd13 分钟前
【工业检测】基于YOLO13-C3k2-EIEM的铸造缺陷检测与分类系统_1
人工智能·算法·分类
Not Dr.Wang4221 小时前
自动控制系统稳定性研究及判据分析
算法
VT.馒头1 小时前
【力扣】2722. 根据 ID 合并两个数组
javascript·算法·leetcode·职场和发展·typescript
EnglishJun1 小时前
数据结构的学习(四)---栈和队列
数据结构·学习
ffqws_1 小时前
A*算法:P5507 机关 题解
算法
执着2591 小时前
力扣hot100 - 108、将有序数组转换为二叉搜索树
算法·leetcode·职场和发展
2501_901147831 小时前
学习笔记:单调递增数字求解的迭代优化与工程实践
linux·服务器·笔记·学习·算法
数智工坊1 小时前
【数据结构-特殊矩阵】3.5 特殊矩阵-压缩存储
数据结构·线性代数·矩阵
AI科技星1 小时前
张祥前统一场论核心场方程的经典验证-基于电子与质子的求导溯源及力的精确计算
线性代数·算法·机器学习·矩阵·概率论
kebijuelun1 小时前
ERNIE 5.0:统一自回归多模态与弹性训练
人工智能·算法·语言模型·transformer