码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode#18 4 Sum

时间:2015-08-01 20:26:33      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:

Problem Definition:

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

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, abcd)
  • The solution set must not contain duplicate quadruplets.
    For example, given array S = {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)

Solution:

1)最直接的思路,固定第一个数,变成 3 Sum 问题,进而变成2 Sum 问题;这个想法可以扩展到k-Sum。重复元组的问题通过跳过重复元素来避免。

 1     # @param {integer[]} nums
 2     # @param {integer} target
 3     # @return {integer[][]}
 4     def fourSum(nums, target):
 5         nums.sort()
 6         res=[]
 7         first=0
 8         finalFirst=len(nums)-4
 9         finalSecond=len(nums)-3
10         rightBound=len(nums)-1
11         while first<=finalFirst:
12             nf=nums[first]
13             second=first+1
14             while second<=finalSecond:
15                 left=second+1
16                 right=rightBound
17                 ns=nums[second]
18                 while left<right:
19                     nl=nums[left]
20                     nr=nums[right]
21                     four=nf+ns+nl+nr
22                     if four>target:
23                         right-=1
24                     elif four<target:
25                         left+=1
26                     else:
27                         res+=[nf, ns, nl, nr],
28                         while left<right and nums[left]==nl:
29                             left+=1
30                         while left<right and nums[right]==nr:
31                             right-=1
32                 second+=1
33                 while second<=finalSecond and nums[second]==ns:
34                     second+=1
35             first+=1
36             while first<=finalFirst and nums[first]==nf:
37                 first+=1
38         return res

 

2)HashTable. 数组中元素两两求和,以和为key,对应的value是多个四元组组成的数组,每个四元组保存了和为key的两个元素,及他们的下标。O(n^2)时间形成这个表。

     然后遍历HashTable [O(n)],对key1,找到能够相加得target的key2 [O(1)],把这俩key对应的value内的四元组交叉组合。防止重复是关键。可以用Set(集合)来存储这些交叉后的元组。

 1     # @param {integer[]} nums
 2     # @param {integer} target
 3     # @return {integer[][]}
 4     def fourSum(self, nums, target):
 5         n=len(nums)
 6         if n<4:
 7             return []
 8         vsMap={}     
 9         res=[]
10         for i in range(n):
11             for j in range(i+1,n):
12                 key=nums[i]+nums[j]
13                 vsMap.setdefault(key, [])
14                 vsMap[key].append([nums[i], i, nums[j], j])
15         for key in vsMap:
16             need=target-key
17             if need in vsMap:
18                 pB=vsMap[need]
19             else:
20                 continue
21             pA=vsMap[key]
22             for a in pA:
23                 for b in pB:
24                     if a[1]==b[1] or a[3]==b[3] or a[1]==b[3] or a[3]==b[1]:
25                         continue
26                     cmb=sorted([a[0], a[2], b[0], b[2]])
27                     if cmb not in res:
28                         res+=cmb,     
29         return res

 

LeetCode#18 4 Sum

标签:

原文地址:http://www.cnblogs.com/acetseng/p/4694587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!