☆☆☆https://leetcode.com/problems/3sum/
来源:互联网 发布:运营商网络制式 编辑:程序博客网 时间:2024/06/05 08:36
https://leetcode.com/problems/3sum/
这道题由于之前写过two sum 可以先固定一个 另外两个用类似twosum的方法
但是niddle就不需要了 因为这道题允许有多个解
我没有写另一个函数 直接把代码都写在一起 不过效率很低
class Solution: # @return a list of lists of length 3, [[val1,val2,val3]] def threeSum(self, num): solution=[] num.sort() length=len(num) for i in range(length-2): for j in range(i+1,length-1): x=0-num[i]-num[j] if x in num[j+1:]: templist=[num[i],num[j],x] #templist.sort() if templist not in solution: solution.append(templist) return solution
一会儿再看看有没有别的方法
0 0
- ☆☆☆https://leetcode.com/problems/3sum/
- https://leetcode.com/problems/two-sum/
- https://leetcode.com/problems/candy/
- https://leetcode.com/problems/sqrtx/
- https://oj.leetcode.com/problems/majority-element/
- https://leetcode.com/problems/longest-common-prefix/
- https://leetcode.com/problems/pascals-triangle/
- https://leetcode.com/problems/powx-n/
- https://leetcode.com/problems/reverse-integer/
- https://leetcode.com/problems/plus-one/
- https://leetcode.com/problems/unique-paths/
- https://leetcode.com/problems/palindrome-number/
- https://leetcode.com/problems/permutation-sequence/
- https://leetcode.com/problems/climbing-stairs/
- https://leetcode.com/problems/divide-two-integers/
- https://leetcode.com/problems/add-binary/
- https://leetcode.com/problems/add-two-numbers/
- https://leetcode.com/problems/merge-sorted-array/
- 【ExtJs】折线图
- JAVA笔试题一[转载+解析]
- Java 引用类型扩充
- Rails中文Podcasts【二】
- LeetCode的easy题集合(C++实现)四
- ☆☆☆https://leetcode.com/problems/3sum/
- struts2中配置servlet
- Rails中文Podcasts【三】
- 数组中的逆序对
- OpenCV特征匹配相关结构(KeyPoint&DMatch类型简介)
- java开发基本语法——总结
- 这个时代
- NUBT1475 数位dp(记忆化搜索)
- 【剑指Offer学习】【面试题7:用两个栈实现队列】