18 4Sum
2015-07-20 12:39
288 查看
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[][]} def fourSum(self, nums, target): "if we adopt left-><-right method, the time complexity is O(n**3) which is not so perfect for this problem written in python(http://www.cnblogs.com/zuoyuan/p/3699384.html), thus, we can sacrifice the space for time,O(n**2)" dic,collection={},set() l=len(nums) nums.sort() if l<4: return [] for i in range(l-1): for j in range(i+1,l): if nums[i]+nums[j] not in dic: dic[nums[i]+nums[j]]=[(i,j)] else: dic[nums[i]+nums[j]].append((i,j)) for k in range(l-1): for m in range(k+1,l): tmp=target-nums[k]-nums[m] if tmp in dic: for n in dic[tmp]: if n[0]>m: collection.add((nums[k],nums[m],nums[n[0]],nums[n[1]])) return [list(p) for p in collection]
相关文章推荐
- Nginx源代码分析之网络超时管理(九)
- JDK环境变量配置以及可能出现的问题
- 弹球游戏
- 弹球游戏
- 弹球游戏
- 弹球游戏
- Windows下Sublime Text 默认打开方式问题解决办法
- Qt5该插件机制(7)--插件开发演示示例代码(Lower-level API)
- leetcode 85:Maximal Rectangle
- 借书方案值多少
- Shrink the Symantec Endpoint Protection Manager 12.1 embedded database
- CentOs 6.x 升级 Python 版本
- Flash打包ANE在安卓上使用笔记
- Linux如何控制 CPU 利用率
- php生成过去100年下拉列表的方法
- JQuery 异步处理,ajax post get getJSON...
- mac myEclipse 2015 svn 安装
- 股票学习36(公司财务分析07)
- 股票学习36(公司财务分析)
- 常见的三种Web服务架构