LeetCode 18. 4Sum

xiaoxiao2021-02-28  34

题目

思路

双指针

代码

class Solution: def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ res_list = [] nums.sort() for i in range(len(nums) - 3): 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 start = j + 1; end = len(nums) - 1 while start < end: num = nums[i] + nums[j] + nums[start] + nums[end] if num < target: start += 1 elif num > target: end -= 1 else: res_list.append([nums[i], nums[j], nums[start], nums[end]]) start += 1 while nums[start] == nums[start - 1] and start < end: start += 1 return res_list
转载请注明原文地址: https://www.6miu.com/read-2629648.html

最新回复(0)