18. 四数之和

作者: 花果山松鼠 | 来源:发表于2018-09-04 19:53 被阅读4次

一、题目原型:

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:答案中不可以包含重复的四元组。

二、题目意思剖析:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

三、解题思路:

从二元组。。。三元组。。。现在终于到了四元组。。。。
其实不管是多少元组,都是一样的方法。到最后都转换到left和right两个指针来控制后两位数字。
详细思路具体请参考:15. 三数之和16. 最接近的三数之和

/**
 思路:和三元组相似。我们需要找到 a+b+c+d = t
 */
func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
    
    // 二元组。。。三元组。。。终于到了四元组。。。。
    if nums.count < 4 {
        return []
    }
    else if nums.count == 4 {
        
        if nums[0] + nums[1] + nums[2] + nums[3] == target {
            return [nums]
        }else {
            return []
        }
    }
    else {
        let tempNums: [Int] = nums.sorted{$0<$1}
        let count = tempNums.count
        var fourSum: [[Int]] = Array.init()
        print(tempNums)
        for indexF in 0 ..< count-3 {
            // 剔除首位相同的元素
            if (indexF != 0) && (tempNums[indexF] == tempNums[indexF - 1]){
                continue
            }
            for indexH in indexF + 1 ..< count-2 {
                
                // 剔除第二位相同的元素,添加判断当 indexH 和 indexF不相邻时/
                if (indexH != 0) && (tempNums[indexH] == tempNums[indexH - 1]) && indexH - 1 != indexF  {
                    continue
                }
                
                let tempArray = self.aFunction(numbers: tempNums, begin: indexH + 1, end: count)
                print(tempArray)
                var left:Int = 0
                var right:Int = tempArray.count - 1
                while left < right {
                    // 临时的四个数字之和。
                    let tempTarget = tempNums[indexF] + tempNums[indexH] + tempArray[left] + tempArray[right]
                    print(tempTarget)
                    // 让tempTarget和目标值 target进行比对
                    if tempTarget == target {
                        let tempFourSum = [tempNums[indexF],tempNums[indexH],tempArray[left],tempArray[right]]
                        fourSum .append(tempFourSum)
                        
                        // 如果两个数字相同,我们直接跳到下一个循环。
                        while left < right && tempArray[left] == tempArray[left+1] {
                            left = left + 1
                        }
                        // 如果两个数字相同,我们直接跳到下一个循环。
                        while left < right && tempArray[right] == tempArray[right-1] {
                            right = right - 1
                        }
                        left = left + 1
                        right = right - 1
                    }
                    if tempTarget < target && left < right{
                        left = left + 1
                    }else if tempTarget > target && left < right{
                        right = right - 1
                    }
                }
            }
        }
        return fourSum
    }
}
func aFunction(numbers: Array<Int>, begin: Int, end: Int) -> Array<Int> {
    let newNumbers = Array(numbers[begin..<end])
    return newNumbers
}

四、小结

总提交数 提交结果

本题和15. 三数之和16. 最接近的三数之和都可归为一类处理,如果有困惑的地方可以前后看看这几篇文章哦,解题思路差不多。

小哥哥小姐姐们有其他好的方法请留言哦,有困惑的地方也非常乐意一起探讨呢。😄!

相关文章

  • 【Leetcode算法题】18. 四数之和

    By Long Luo 18. 四数之和[https://leetcode-cn.com/problems/4su...

  • LeetCode-18 四数之和

    题目:18. 四数之和 难度:中等 分类:数组 解决方案:双指针 今天我们学习第18题四数之和,这是一道中等题。像...

  • 力扣每日一题:18.四数之和

    18.四数之和 https://leetcode-cn.com/problems/4sum/[https://le...

  • 18.四数之和

    18.四数之和 题目链接:https://leetcode-cn.com/problems/4sum/[https...

  • 18. 四数之和

    一、题目原型: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四...

  • 18. 四数之和

    知乎ID: 码蹄疾码蹄疾,毕业于哈尔滨工业大学。小米广告第三代广告引擎的设计者、开发者;负责小米应用商店、日历、开...

  • 18. 四数之和

    18.四数之和 给定一个包含n个整数的数组nums和一个目标值target,判断nums中是否存在四个元素a,b,...

  • 18.四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,...

  • 18. 四数之和

    一、题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素...

  • 18.四数之和

    自己解法 四数之和解题思路和三数之和类似,不过这个方式是固定前两个数字,后面两个数字用夹逼的方式向中间逼近,这样时...

网友评论

    本文标题:18. 四数之和

    本文链接:https://www.haomeiwen.com/subject/leynwftx.html