leetcode 4sum python
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
numLen=len(nums)
if numLen <= 3:
return list()
nums.sort()
res,d=set(),{}
for p in xrange(numLen):
q=p+1
while q < numLen:
if nums[p]+nums[q] not in d:
d[nums[p]+nums[q]] = [(p,q)]
else:
d[nums[p]+nums[q]].append((p,q))
q+=1
for i in xrange(numLen):
j=i+1
while j < numLen-2:
tmp = target-nums[i]-nums[j]
if tmp in d:
for k in d[tmp]:
if k[0] > j:
res.add( ( nums[i], nums[j], nums[k[0]], nums[k[1]] ) )
j+=1
return [list(i) for i in res]
@link http://chaoren.is-programmer.com/posts/45308.html
leetcode 4sum python的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- Cannot convert '0000-00-00 00:00:00' to TIMESTAMP
在url上添加参数 zeroDateTimeBehavior=convertToNull jdbc:mysql://localhost/myDatabase?zeroDateTimeBehavior= ...
- js方法在对象中的状态
在C#中,只有对象的字段存储在堆中,而方法则存储在一个方法表中.当实例化多个对象时,为字段分配了内存空间,而方法都指向一个方法表中的同一个方法. 如 而在JS中,字段和方法都属于值类型,都存储在堆中. ...
- JavaScript事件处理程序 学习笔记
我一直认为Javascript的特点就是在和用户交互的过程中可以进行一些操作,那么事件作为用户交互的主要部分就显得特别重要,今天先学习了JS事件处理程序的相关内容. 首先,要明白Javascript ...
- VS2012 黑色护眼主题
在黑色主题基础上,更改了字体 Ms Comic Sans 字号也增大了 附件中有两个 一个是原版主题下载自https://studiostyl.es/ 第二个是如下改完后的主题 vssettings. ...
- c#中的数据类型简介(string)
Sting 字符串 引入话题 字符串是一个引用类型,从string数据类型的代码定义中也可以看出它实现了IEnumerable<char>接口和IEnumerable接口,因此字符串可以看 ...
- python成长之路第一篇(5)文件的基本操作
一.三元运算 我们在上章学习的if,,else,,有一种简便的方法 他的表达式是这样的:变量 = 值1 if 条件 else 值2 解释过来就是如果aaa等于sss则输出值1否则输出值2 二.类的概念 ...
- display属性值
display属性值:none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元素前后没有换行符. inline-bl ...
- 几个关于JPEGLIB库的博客
1.http://blog.csdn.net/huxiangyang4/archive/2010/07/12/5728888.aspx 我认为是最好的 2.http://blog.csdn.net/a ...
- poj3673---双重for循环
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 15 int main ...
- poj3094---对字符串的处理
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; int len, ...