15.三数之和

1.题目描述

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。

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

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。

示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

提示:

  • 3 <= nums.length <= 3000
  • -105 <= nums[i] <= 105

2.解题思路

先对数组进行排序,使用一个for循环其中i枚举第一个数的所有情况,对于另外两个数,使用双指针,left指向i+1的位置,right指向n-1的位置,内层循环找所有使得三个数和为0的情况,并将结果记录下来

其中,关键点在于如何去除重复出现的组合:

其中,对于第一个数的去重,当i > 0 且 nums[i] == nums[i-1]时,表示这个数已经遍历过,无需重复遍历后序组合,直接continue跳过

对于第二个和第三个数的去重,时机应该在找到了符合nums[i]+nums[left]+nums[right]==0的情况下,如果此时nums[left+1]==nums[left],需要直接令left+1,表明第二个数无需重复组合,同理当nums[right]==nums[right-1]也需要直接令right-1,表明第三个数无需重复组合。 

3.代码实现

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        Arrays.sort(nums);
        //固定一个数,另外两个数使用双指针遍历
        for (int i = 0; i < n; i++) {
            //如果最小的一个数都已经大于0,说明后序不再有可行解,提前返回
            if (nums[i] > 0) {
                return res;
            }
            //对第一个数去重
            if (i > 0 && nums[i] == nums[i-1]) {
                continue;
            }
            int left = i + 1;
            int right = n - 1;
            while (right > left) {
                int temp = nums[i] + nums[left] + nums[right];
                if (temp > 0) {
                    right -= 1;
                } else if (temp < 0) {
                    left += 1;
                } else {
                    res.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    //分别对left,right去重
                    while (left < right && nums[left] == nums[left + 1]) {
                        left += 1;
                    }
                    while (right > left && nums[right] == nums[right - 1]) {
                        right -= 1;
                    }
                    left += 1;
                    right -= 1;
                }
            }
        }
        return res;
    }
}

### C++ 实现三数之和问题 对于寻找数组中三个数相加等于零的问题,可以采用双指针方法来优化暴力求解的时间复杂度。具体来说,在遍历过程中固定一个数值作为目标值的相反数,然后利用两个指针对剩余部分进行扫描。 #### 排序与去重 首先对输入数组 `nums` 进行升序排列以便后续操作,并去除重复元素以减少不必要的计算量: ```cpp std::sort(nums.begin(), nums.end()); auto last = std::unique(nums.begin(), nums.end()); nums.erase(last, nums.end()); ``` #### 双指针查找组合 接着定义外层循环用于选取当前考察的目标值;内嵌两层循环分别指向待查区间的两端,逐步向中间靠拢直至找到满足条件的一组解或将要交错为止: ```cpp for (int i = 0; i < nums.size(); ++i) { int target = -nums[i]; int start = i + 1; int end = nums.size() - 1; while (start < end) { if (nums[start] + nums[end] == target) { result.push_back({nums[i], nums[start], nums[end]}); // 跳过相同元素防止重复记录 while (start < end && nums[start] == nums[++start]); while (start < end && nums[end] == nums[--end]); } else if (nums[start] + nums[end] < target) { ++start; } else { --end; } } // 避免再次选到相同的起始点 while (i + 1 < nums.size() && nums[i + 1] == nums[i]) ++i; } ``` 此段代码实现了高效的三数之和查询功能[^1]。为了保证结果集中不含冗余项,在每次成功匹配后均需跳过连续出现的相同数字序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值