leetcode 018 四数之和 4sum python 多种解法

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
'''

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]


'''



class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:

        # Approach one    O(n3)
        # from collections import Counter
        # dic = Counter(nums)
        # res = []
        # nums = sorted(nums)
        # length = len(nums) - 1
        # for i in range(length-2):
        #     if i != 0 and nums[i] == nums[i - 1]: continue     # 避免重复答案
        #     key = target - nums[i]
        #     for j in range(i + 1, length-1):
        #         if j != i + 1 and nums[j] == nums[j - 1]: continue   # 避免重复答案
        #         twosum = key - nums[j]
        #         l,r = j+1 , length
        #         while l < r:
        #             ans = nums[l] + nums[r]
        #             if twosum < ans:
        #                 r -= 1
        #             elif twosum == ans:
        #                 res.append([nums[i],nums[j],nums[l],nums[r]])
        #                 r -= 1
        #                 while nums[r] == nums[r + 1] and l < r:
        #                     r -= 1
        #                 l += 1
        #                 while nums[l] == nums[l - 1] and l < r:
        #                     l += 1
        #             else:
        #                 l += 1
        # return res



        # Approach two  O(n2)
        res = set()
        nums = sorted(nums)
        length = len(nums)
        dic = {}
        if length < 4 or 4 * nums[0] > target or 4 * nums[-1] < target: return []
        for i in range(length-1):
            for j in range(i+1,length):
                twosum = nums[i] + nums[j]
                if twosum in dic.keys():
                    dic[twosum].append((i,j))
                else:
                    dic[twosum] = [(i,j)]
        for i in range(length-1):
            for j in range(i+1,length):
                ans = target - nums[i] - nums[j]
                if dic.get(ans, 0) != 0:
                    for k in dic.get(ans):
                        if k[0] > j:
                            res.add((nums[i] , nums[j] , nums[k[0]] , nums[k[1]] ))
        return list(res)

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值