[LeetCode][Python]18: 4Sum
# -*- 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的更多相关文章
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- LeetCode:18. 4Sum(Medium)
1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...
- 【一天一道LeetCode】#18. 4Sum
一天一道LeetCode (一)题目 Given an array S of n integers, are there elements a, b, c, and d in S such that ...
- 《LeetBook》leetcode题解(18) : 4Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
- 【LeetCode】18. 4Sum
题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...
- [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 ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
随机推荐
- javascript 数据结构和算法读书笔记 > 第二章 数组
这章主要讲解了数组的工作原理和其适用场景. 定义: 一个存储元素的线性集合,元素可以通过索引来任意存取,索引通常是数字,用来计算元素之间存储位置的偏移量. javascript数组的特殊之处: jav ...
- web2py官方文档翻译
00前言 我相信能够轻松地构建高质量增长的web应用程序是至关重要的一个自由和开放的社会.这可以防止玩家最大的垄断信息的流通. 因此我从2007年开始web2py项目,主要是作为一种教学工具与简化we ...
- 安装程序时出现错误代码0x80070422
通过win10应用商店,下载应用,安装时出现错误代码0x80070422. 需要打开services.msc,将windows update服务打开.
- 离线使用nuget
先新建一个项目,将所有想保存下来或者要升级的package先安装或升级. 然后在项目中将packages文件夹全部拷贝出来,专门放到一个目录备用,以后的项目就可以根据此packages文件夹来离线使用 ...
- python计算md5值
from hashlib import md5 m = md5(') print m.hexdigest()
- Windows 打开防火墙上的指定端口
第一步: 控制面板-->打开防火墙 第二步: 高级设置 第三步: 入站规则--->新建规则 第四步: 第五步: 第六步: 第七步: 第八步:
- CSS随记
在CSS中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.如果浮动非替换元素,则要指定一个明确的宽度:否则,它们会尽可能地窄. 注释:float属性不具有继承特性,就是说子元素 ...
- 第52周二Restful
今天去spring官网发现一个关键词:Restful,以前只在与一个系统对接时用到过这种形式的接口,但印象不深,百度搜索后才感觉自己太out了,这个概念2000年提出,2009年时国内就有很多人推荐使 ...
- mysql主从同步错误解决和Slave_IO_Running: NO
1.出现错误提示. Slave I/O: error connecting to master 'backup@192.168.1.x:3306' - retry-time: 60 retries: ...
- 可用的CSS文字两端对齐
最近在工作项目中接触到Web界面设计的问题,要实现文字两端对齐的效果.在网上搜索了一下,用的都是类似的技巧: text-align:justify;text-justify:inter-ideogra ...