# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 18: 4Sum
https://oj.leetcode.com/problems/4sum/ 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, a ≤ b ≤ c ≤ d)
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) ===Comments by Dabay===
k sum的问题就可以转化为k-1 sum,直到3 sum。
这道题可以使用空间来换时间,用一个hash表来记录两个数字的和,值存他们的坐标。
然后两次循环,判断是否在hash中是否有命中的值满足和为target。
如果有的话,因为两次循环是从小到大,应该要满足hash表的小坐标大于内循序的坐标。
'''
class Solution:
# @return a list of lists of length 4, [[val1,val2,val3,val4]]
def fourSum(self, num, target):
d = {}
num.sort()
for i in xrange(len(num)-1):
for j in xrange(i+1, len(num)):
sum2 = num[i]+num[j]
if sum2 not in d:
d[sum2] = [[i,j]]
else:
d[sum2].append([i,j]) res = []
for i in xrange(len(num)-3):
for j in xrange(i+1, len(num)-2):
x = target - (num[i] + num[j])
if x in d:
for (m,n) in d[x]:
if m > j and [num[i],num[j],num[m],num[n]] not in res:
res.append([num[i],num[j],num[m],num[n]])
return res def main():
sol = Solution()
nums = [-2, -1, 0, 0, 1, 2]
print sol.fourSum(nums, 0) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]18: 4Sum的更多相关文章

  1. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  2. LeetCode:18. 4Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...

  3. 【一天一道LeetCode】#18. 4Sum

    一天一道LeetCode (一)题目 Given an array S of n integers, are there elements a, b, c, and d in S such that ...

  4. 《LeetBook》leetcode题解(18) : 4Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  6. 【LeetCode】18. 4Sum

    题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...

  7. [LeetCode] 18. 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

随机推荐

  1. Android_实现静默安装和卸载应用

    转:http://www.cnblogs.com/ondream/archive/2012/04/13/2446138.html 前段时间做了一个批量安装卸载应用程序的小应用,由于安装卸载应用程序的部 ...

  2. jQuery二级联动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. return 和 echo 的小坑

    在写项目的时候,有好几次遇到过同样的问题,控制器里面返回的json在前台无显示,利用console.log()总是显示这样的现象 数据库操作成功,却没有返回值. 原因是在控制器返回的使用使用了 ret ...

  4. @Html.ActionLink 添加样式 html标签

    @Html.ActionLink(item.MessageTitle, "Detail", "News",new { MessageId = item.Mess ...

  5. class属性多个样式的用法

    今天看到一个非常好用的样式用法,给已经在睡梦中苏醒的你们来一段代码头脑风暴.大家都知道现在div+css布局的使用已经不是可以用潮流来概括的了,换个词应该是:普及.以前的表格布局现在是少之极少,因为表 ...

  6. Codeforces 241B

    因为博客园的公式编辑有点坑,所以-- 原题

  7. 百度静态资源(JS)公共库

         例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js   classlist http://apps ...

  8. c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow

    c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow How to use wst ...

  9. 将Dictionary序列化为json数据 、json数据反序列化为Dictionary

    需要引用System.Web.Extensions  dll类库 /// <summary> /// 将json数据反序列化为Dictionary /// </summary> ...

  10. hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...