【Leetcode】Python实现四数之和

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

第一次使用下面代码,出现超时,提交不了

dicti.keys()返回的是一个 list,判断一个元素是不是在一个 list 里是 O(n)的。这里直接 if exp in dicti 就可以了,判断一个元素是不是在一个 dict 里是 O(1)的。

所以复制下面代码的时候只要把.keys()删掉就可以了

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        # res为集合,可以去重
        res, dic = set(), {}
        # 获取数组nums的长度
        num_len = len(nums)
        # 排序,无返回值
        nums.sort()
        # 遍历0-num_len
        for i in range(num_len):
            # 遍历1到num_len
            for j in range(i+1, num_len):
                # 获取两者和
                key = nums[i] + nums[j]
                # 判断是否在dic字典中,不在的话,则把元组类型i,j的列表加入字典
                if key not in dic.keys():
                    dic[key] = [(i, j)]
                else:
                    # 在dic中则加入相应key的列表中
                    dic[key].append((i, j))
        # 遍历0-num_len
        for i in range(num_len):
            # 遍历1-num_len-2
            for j in range(i+1, num_len-2):
                # exp作为目标值和两者的差,需要去dic中判断是否存在
                exp = target - nums[i] - nums[j]
                # 判断差值exp是否存在dic字典的keys中
                if exp in dic.keys():
                    # 在dic中则遍历dic[exp]
                    for tmpIndex in dic[exp]:
                        # 如果tmpIndex[0]代表的i 大于 j 意味着四个值没有重复,加入结果结合中
                        if tmpIndex[0] > j:
                            # 集合会去重
                            res.add((nums[i], nums[j], nums[tmpIndex[0]], nums[tmpIndex[1]]))
        # 对结果进行列表化
        return [list(i) for i in res]


if __name__ == '__main__':
    s = Solution()
    print(s.fourSum([1, 0, -1, 0, -2, 2], 0))

猜你喜欢

转载自blog.csdn.net/chenhua1125/article/details/80773393