LeetCode 3Sum

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if nums is None:
            return []
        len_nums = len(nums)
        if len_nums < 3:
            return []
        nums.sort()

        list_result = []
        index_st = 0
        index_end = len_nums - 1

        index_fix = 0
        while(index_fixif index_fix != 0 and (nums[index_fix-1] == nums[index_fix]):
                index_fix += 1
                continue

            index_st = index_fix + 1
            index_end = len_nums - 1

            while(index_stif sum == 0:
                       list_result.append([nums[index_st],nums[index_fix],nums[index_end]])
                       stnum = nums[index_st]
                       ednum = nums[index_end]
                       while(nums[index_st]==stnum and index_st1
                       while(nums[index_end]==ednum and index_end>index_st):
                           index_end -= 1
                    elif sum > 0:
                        index_end -= 1
                    else:
                        index_st += 1

            index_fix += 1
        return list_result

Runtime: 184 ms

你可能感兴趣的:(算法)