1. 程式人生 > >leetcode -18.4Sum

leetcode -18.4Sum

我的解法是利用2Sum相同的演算法:

1. 對原陣列排序

2. 設定首尾兩個指標。當兩數的和大於target時,尾指標向左移一位;當兩數的和小於target時,尾指標向右移一位。

對於4Sum的解法,因為是四個數字,利用兩個迴圈陣列確定前兩個數字,剩下的兩個數字和target便可以使用2Sum的演算法完成。但是演算法的執行時間很長。

程式碼如下

class Solution:
    def fourSum(self, nums, target):
        ans = []
        nums.sort()
        for i in range(len(nums)-2):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            for j in range(i+1,len(nums)-2):
                if j > i+1 and nums[j] == nums[j-1]:
                    continue
                b, e = j + 1, len(nums) - 1
                while b < e:
                    diff = target - (nums[i] + nums[j] + nums[b] + nums[e])
                    if diff > 0:
                        b += 1
                    elif diff < 0:
                        e -= 1
                    else:
                        ans.append([nums[i], nums[j], nums[b], nums[e]])
                        while b < e and nums[b] == nums[b + 1]:
                            b += 1
                        while b < e and nums[e] == nums[e - 1]:
                            e -= 1
                        b += 1
                        e -= 1
        return ans

在discuss區中的python最快程式碼如下:

def fourSum(self, nums, target):
    def findNsum(nums, target, N, result, results):
        if len(nums) < N or N < 2 or target < nums[0]*N or target > nums[-1]*N:  # early termination
            return
        if N == 2: # two pointers solve sorted 2-sum problem
            l,r = 0,len(nums)-1
            while l < r:
                s = nums[l] + nums[r]
                if s == target:
                    results.append(result + [nums[l], nums[r]])
                    l += 1
                    while l < r and nums[l] == nums[l-1]:
                        l += 1
                elif s < target:
                    l += 1
                else:
                    r -= 1
        else: # recursively reduce N
            for i in range(len(nums)-N+1):
                if i == 0 or (i > 0 and nums[i-1] != nums[i]):
                    findNsum(nums[i+1:], target-nums[i], N-1, result+[nums[i]], results)

    results = []
    findNsum(sorted(nums), target, 4, [], results)
    return results

這個程式碼核心也是使用2Sum的演算法,使用遞迴可解任意個數的NSum問題。但是速度快到只要120ms,問題在於少做了很多次對於重複數字的判斷。