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 文中代码为了不动官网提供的初始 ...
随机推荐
- gateone安装(web版本ssh)
前言: 好久都没来写博客,最近忙啥去了呢? 一是忙于saltstack的二次开发,二是云计算的学习研究中,所以就一直没写东西,今天给大家介绍个工具. 好了,开始正文! 1.首先来说一下为什么要web ...
- C# 整个网页保存成图片
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- JavaScript 【非IE DOM2级XML】
DOM2中的XML IE可以实现了对XML字符串或XML文件的读取,其他浏览器也各自实现了对XML处理功能.DOM2级在document.implementaion中引入了createDocument ...
- Redis系列整理
0.Redis系列-安装部署维护篇 1.Redis系列-远程连接redis并给redis加锁 2.Redis系列-存储篇string主要操作函数小结 3.Redis系列-存储篇list主要操作函数小结 ...
- visual assist常用快捷键
转自http://my.oschina.net/u/211101/blog/127822 一些打开啊新建就不说了…… //先来个我自己最喜欢的,经常不用,老忘记,以前eclipse最喜欢这个快捷键了 ...
- 收MUD巫师学徒,MUD开发,LPC语言开发
收MUD巫师学徒,MUD开发,LPC语言开发 对这个有兴趣的联系我,签订协议 Q 184377367
- Smarty3配置
下载Smarty压缩包并解压,复制其中的libs文件夹到我们的PHP工程目录下(可将其改名为smarty).同时,在工程目录下新建三个文件夹,分别取名为templates.templates_c和sm ...
- Jquery 解决 H5 placeholder元素问题
<style type="text/css"> .placeholder{ color: #cacaca; } </style> <script ty ...
- Java之线程的控制
1. join线程: 在线程执行过程中,有时想让另一个线程先执行,比如将一大问题分割成许多小问题,给每一个小问题分配线程,但所有小问题处理完后再让主线程进一步操作.此时我们可以在主线程中调用其它线程的 ...
- React 从0开始 消息传递
React笔记 React 数据决定DOM 以往的做法是通过JS去操作DOM 将数据填充 JSX Jsx javascript xml HTML的结构组装到js中 jsx使用style的时候 不能直接 ...