python solution


  • 0
    1

    class Solution(object):
    def threeSum(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    nums = sorted(nums)
    outList = []
    i = 0
    nextValue = ''
    while i < len(nums)-2:
    nowValue = nums[i]
    if nowValue == nextValue:
    i += 1
    nextValue = nowValue
    continue
    j, k = i+1, len(nums)-1
    while j<k:
    thisValue = nums[i]+nums[j]+nums[k]
    if thisValue>0:
    k -= 1
    elif thisValue<0:
    j += 1
    else:
    oneList = [nums[i], nums[j], nums[k]]
    outList.append(oneList)
    while j<k and nums[j+1]==nums[j]:
    j += 1
    while j<k and nums[k-1]==nums[k]:
    k -= 1
    k -= 1
    j += 1
    nextValue = nums[i]
    i += 1
    return outList


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.