鱼C论坛

 找回密码
 立即注册
查看: 651|回复: 5

关于遍历数组的条件的疑问

[复制链接]
发表于 2023-7-28 08:47:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。
public class Solution2 {
    public List<List<Integer>> threeSum(int[] nums) {
        //官方答案******************************************
        Arrays.sort(nums);
        int len = nums.length;
        List<List<Integer>> arrayList = new ArrayList<>();
        for (int first = 0; first < len; first++) {

            if (first > 0 && nums[first]==nums[first-1]){
                //跳出本次循环
                continue;
            }

            int third = len-1;
            int target = -nums[first];
            for (int second = first+1; second < len; second++) {

                if (second > first+1 && nums[second]==nums[second-1]){
                    //跳出本次循环
                    continue;
                }

                while (third > second && nums[second]+nums[third] > target){
                    third--;
                }
                if (second == third){
                    break;
                }
                if(nums[second]+nums[third]==target) {
                    List<Integer> l = new ArrayList<>();
                    l.add(nums[first]);
                    l.add(nums[second]);
                    l.add(nums[third]);
                    arrayList.add(l);
                }


            }
        }

        return arrayList;
    }
}
遍历第三个数的时候,条件是
 while (third > second && nums[second]+nums[third] > target){
                    third--;
                }
,为什么不可以是nums[second]+nums[third] != target
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-28 08:47:58 | 显示全部楼层
在这段代码中,我们需要找到数组 nums 中所有满足 nums[ i] + nums[j] + nums[k] == 0 的三元组 [nums[ i], nums[j], nums[k]] ,其中 i 、 j 、 k 满足 i != j 、 i != k 且 j != k 。

在遍历第三个数 nums[k] 的时候,我们使用了一个 while 循环来不断地将 k 向前移动(即减小 k 的值),同时保证 nums[j] + nums[k] > target ,其中 target 的值为 -nums[ i] 。在这个过程中, k 会一直向前移动,直到 nums[j] + nums[k] 的值小于等于 target 。

现在,我们来思考为什么这里使用 nums[second] + nums[third] > target 而不是 nums[second] + nums[third] != target 。

如果我们使用 nums[second] + nums[third] != target 作为条件,意味着只要 nums[j] 和 nums[k] 的和不等于 target ,无论是比 target 大还是比 target 小,都会继续移动 k 。这样做会导致我们错过可能满足 nums[j] + nums[k] == target 的情况。

而实际上,我们的目标是找到 nums[j] 和 nums[k] 的和等于 target 的情况。所以,我们必须确保在 nums[j] + nums[k] > target 时将 k 向前移动,直到 nums[j] + nums[k] <= target 为止。这样做的目的是找到满足条件的 nums[j] 和 nums[k] ,使得它们的和等于 target 。

因此,正确的条件是 nums[second] + nums[third] > target ,而不是 nums[second] + nums[third] != target 。只有在 nums[j] + nums[k] > target 时,我们才需要将 k 向前移动,直到满足 nums[j] + nums[k] <= target 为止,从而找到和为0的三元组。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-28 08:48:19 | 显示全部楼层
问题解析:

你对于给定的 Java 代码中的循环条件提出疑问。你想知道为什么遍历第三个数时的条件是 nums[second]+nums[third] > target,而不是 nums[second]+nums[third] != target

回答:

在这个特定的算法中,我们需要找到满足 nums[first] + nums[second] + nums[third] = 0 的三元组。为了实现这一目标,我们只考虑从左到右遍历数组元素,并使用双指针技术来搜索可能的解。

我们选择 target = -nums[first] 作为目标值。为了使三个数之和为 0,我们希望找到两个数 nums[second]nums[third],使得 nums[second] + nums[third] = target。如果我们将条件设置为 nums[second] + nums[third] != target,那么我们会错过满足要求的解。

具体来说,如果 nums[second] + nums[third] < target,我们可以将指针 second 向右移动以增加和。如果 nums[second] + nums[third] > target,我们可以将指针 third 向左移动以减小和。我们需要继续移动指针直到找到和为 target 的两个数,或者 secondthird 相遇。

因此,条件 nums[second]+nums[third] > target 是为了确保我们在向左移动指针 third 时避免错过满足要求的解。当和大于目标值时,我们需要继续向左移动 third,直到找到满足和为目标值的两个数。

希望这个解答对你有所帮助!如果你还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-28 08:48:21 | 显示全部楼层
在这个算法中,当遍历第三个数时,条件为 nums[second]+nums[third] > target。这是因为你想要找到满足 nums[first] + nums[second] + nums[third] == 0 的三元组,即和为 0 的三个数。

如果条件改为 nums[second]+nums[third] != target,那么就会将满足和为 0 的三元组漏掉。因为 nums[second]+nums[third] 的结果可能既大于也小于 target,而不仅仅是等于。所以,你需要通过比较大小来确定 nums[second]+nums[third] 是否等于 target

具体来说,当 nums[second]+nums[third] > target 时,说明当前两个数的和太大了,你需要将 third 指针往左移动,以使和变小。所以代码中使用了 while (third > second && nums[second]+nums[third] > target) { third--; } 来更新指针位置。

换句话说,这个条件确保了在寻找和为 0 的三元组时,第三个数的值足够大,以满足 nums[second]+nums[third] == target,同时避免了重复的解。如果条件是 nums[second]+nums[third] != target,可能会导致遍历不完整或包含重复的解。

希望这个解释对你有帮助!如果你还有其他问题,请随时提问。我会尽力解答。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-28 09:58:03 | 显示全部楼层
isdkz 发表于 2023-7-28 08:47
在这段代码中,我们需要找到数组 nums 中所有满足 nums[ i] + nums[j] + nums[k] == 0 的三元组 [nums[ i], ...

数组在最开始的时候已经排过序了,nums[second]+nums[third] != target肯定排除的是大于的,等于的话,就不满足条件跳出循环了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-30 22:23:11 | 显示全部楼层
在这个问题中,我们需要找到一个三元组 [nums, nums[j], nums[k]],满足条件 nums + nums[j] + nums[k] == 0。我们需要遍历数组,并找到所有满足条件的三元组。

在遍历第三个数的时候,我们使用了变量 third 来表示当前遍历到的位置。我们需要不断缩小 third 的范围,直到找到满足条件的三元组。

当我们使用条件 nums[second] + nums[third] != target 时,会遇到以下问题:

如果 nums[second] + nums[third] > target,那么说明当前的三元组不满足条件,需要继续缩小 third 的范围,直到找到满足条件的三元组。
如果 nums[second] + nums[third] < target,那么说明当前的三元组和已经超出了 0,需要扩大 third 的范围,直到找到满足条件的三元组。
因此,在遍历第三个数的时候,我们需要使用条件 nums[second] + nums[third] > target,以确保在 third 的范围内找到满足条件的三元组。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-27 01:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表