LeetCode-python 18.四数之和

题目链接
难度:中等       类型: 数组


给定一个包含 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]
]

解题思路


1.排序
2.在三数之和
的外面加一层循环
遇到相同的数要跳过,以免重复

代码实现

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        nums = sorted(nums)
        res = []
        len_nums = len(nums)
        for i in range(len_nums-3):
            if i-1>=0 and nums[i]==nums[i-1]:
                continue
            for j in range(i+1, len_nums-2):
                if j-1>i and nums[j] == nums[j-1]:
                    continue
                start = j+1
                end = len_nums-1
                while start<end:
                    sum = nums[i] + nums[j] + nums[start] + nums[end]
                    if sum == target:
                        res.append([nums[i],nums[j], nums[start], nums[end]])
                        while start<end and nums[start]==nums[start+1]:
                            start += 1
                        while start<end and nums[end] == nums[end-1]:
                            end -= 1
                        start += 1
                        end -= 1
                    elif sum > target:
                        end -= 1
                    else:
                        start += 1
                    
        return res

本文链接:https://www.jianshu.com/p/d13071cd072a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容