码迷,mamicode.com
首页 > 编程语言 > 详细

Leetcode 15. 3Sum(python)

时间:2016-04-05 19:28:02      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

先排序,再循环遍历,用双指针。

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        solution=[]
        nums.sort()
        for i in range(len(nums)-1):
            left=i+1
            right=len(nums)-1
            while left<right:
                val=nums[i]+nums[left]+nums[right]
                if val==0 and [nums[i],nums[left],nums[right]] not in solution:
                    solution.append([nums[i],nums[left],nums[right]])
                    left+=1
                    right-=1
                elif val<0:
                    left+=1
                else:
                    right-=1
        return solution

        

  

Leetcode 15. 3Sum(python)

标签:

原文地址:http://www.cnblogs.com/colorss/p/5356025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!