LeetCode 18. Sum of four numbers
- Title:
Give you an array of n integers, nums, and a target value, target. Please find and return the quads [nums[a], nums[b], nums[c], nums[d]]:
- 0 <= a, b, c, d < n
- a. b, c and d are different from each other
- nums[a] + nums[b] + nums[c] + nums[d] == target
You can return answers in any order.
- Example:
Example 1 : Input: nums = [1,0,-1,0,-2,2], target = 0 Output:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2 : Input: nums = [2,2,2,2,2], target = 8 Output:[[2,2,2,2]]
- Tips:
- 1 <= nums.length <= 200
- − 1 0 9 ≤ n u m s [ i ] ≤ 1 0 9 -10^9\le nums[i]\le 10^9 −109≤nums[i]≤109
- − 1 0 9 ≤ t a r g e t ≤ 1 0 9 -10^9\le target\le 10^9 −109≤target≤109
- Code 1:
class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: result = [] if len(nums) < 4: return result nums.sort() length = len(nums) for i in range(length-3): if i > 0 and nums[i] == nums[i-1]: continue if nums[i] + nums[length-3] + nums[length-2] + nums[length-1] < target: continue if nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target: break for j in range(i+1,length-2): if j > i+1 and nums[j] == nums[j-1]: continue if nums[i] + nums[j] + nums[j+1] + nums[j+2] > target: break if nums[i] + nums[j] + nums[length-1] + nums[length-2] < target: continue left = j + 1 right = length - 1 while left < right: temp = nums[i] + nums[j] + nums[left] + nums[right] if temp == target: result.append([nums[i],nums[j],nums[left],nums[right]]) while left < right and nums[left] == nums[left+1]: left += 1 left += 1 while left < right and nums[right] == nums[right-1]: right -= 1 right -= 1 elif temp < target: left += 1 else: right -= 1 return result
- Algorithm description:
Summary: two for loops are used to determine the first two numbers in the four numbers, and the left and right pointers are used to determine the last two numbers. In order to meet the requirements of time complexity, avoid enumerating to duplicate quads as far as possible. Therefore, when sorting the array nums, do the following in the loop:
(1) The subscript enumerated in each loop must be greater than the subscript enumerated in the previous loop;
(2) In the same loop, if the current element is the same as the previous element, the current element is skipped.
- First, judge whether the length of array nums meets the conditions. If not, return an empty list;
- The first for loop determines the subscript of the first number as i, the second for loop determines the subscript of the second number as J, the two subscripts meet i < J, and the left and right pointers point to subscript j+1 and length-1 respectively;
- Calculate the sum temp of four numbers each time, and perform the following operations:
① If temp == target, add the four enumerated numbers to the result, then move the left pointer to the right until different numbers are encountered, and move the right pointer to the left until different numbers are encountered;
② If temp < target, move the left pointer to the right one bit;
③ If temp > target, move the right pointer to the left by one bit; - Among them, there are pruning operations in each cycle:
① After determining the first number, if num [i] + num [J] + num [J + 1] + num [J + 2] > target, it means that no matter what value is taken for the remaining three numbers, the sum of the four numbers must be greater than the target, so exit the first cycle;
② After determining the first number, if num [i] + num [length-3] + num [length-2] + num [length-1] < target, it means that no matter what value is taken for the remaining three numbers, the sum of the four numbers must be less than the target. Therefore, the first cycle directly enters the next round, enumerating num [i + 1];
③ After determining the second number, if num [i] + num [J] + num [J + 1] + num [J + 2] > target, it means that no matter what value is taken for the remaining two numbers, the sum of the four numbers must be greater than the target, so exit the second cycle;
④ After determining the second number, if num [i] + num [J] + num [length-1] + num [length-2] < target, it means that no matter what value the remaining two numbers take, the sum of the four numbers must be less than the target. Therefore, the second cycle directly enters the next round to enumerate num [J + 1]
Carrot
October 22, 2021 23:10:55
I don't know where I'm going, but I'm on my way! |
---|
Time is in a hurry. Although we haven't met, we met here. It's really great fate. Thank you for your visit! |