Today begin formal title LeetCode brush journey, its own programming algorithm capabilities are bad, so I decided to carry on training hard, and a solution found in the code, optimization, and personal understanding, that's all.
Column - [LeetCode]
LeetCode subject classification summary
Given a n array of integers and a target nums target, if there are four elements a, b, c, and d nums determined such that a + b + c + d is equal to the value of the target? Identify all satisfy the conditions of the quad and do not repeat.
Note: The answer can not contain duplicate quad.
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
Problem solution 1:
Violent dismantling process is really very simple! ! ! As you know, however, this approach is time complexity is O (n ^ 4). . . Are substantially exceeded the time limit.
class Solution:
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
result = []
for i, a in enumerate(nums):
for j, b in enumerate(nums[i + 1:]):
for k, c in enumerate(nums[j + i + 2:]):
for _, d in enumerate(nums[i + j + k + 3:]):
if a + b + c + d == 0:
result.append([a, b, c, d])
return result
Problem solution 2:
How to improve the speed? We can c+d
Each result is added to the lookup table, so we only need to traverse the a\b
two arrays, then look in the lookup table 0 - a - b
can solve the problem.
You need to create a map
store nums
all the different two numbers and then records it in the nums
coordinate position.
class Solution:
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
nums.sort()
result = list()
nums_len = len(nums)
if nums_len < 4:
return result
if nums[0] * 4 > target or nums[nums_len - 1] * 4 < target:
return result
nums_map = {}
for i in range(nums_len-1, 0, -1):
if i < nums_len - 1 and nums[i] == nums[i + 1]:
continue
for j in range(i-1, -1, -1):
if j < i-1 and nums[j] == nums[j + 1]:
continue
if nums[i] + nums[j] not in nums_map:
nums_map[nums[i] + nums[j]] = [[j, i]]
else:
nums_map[nums[i] + nums[j]].append([j, i])
for i in range(nums_len - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i+1, nums_len - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
dif = target - nums[i] - nums[j]
if dif not in nums_map:
continue
else:
for num in nums_map[dif]:
if num[0] > j:
result.append([nums[i], nums[j], nums[num[0]], nums[num[1]]])
return result
3 solution to a problem:
Solve the problem by double pointer the way, most of the sum converted into 2-sum, but more difficult to think of, can be obtained by experience or training post-optimization.
class Solution(object):
def fourSum(self, nums, target):
def findNsum(l, r, target, N, result, results):
if r - l + 1 < N or N < 2 or target < nums[l] * N or target > nums[r] * N:
return
# two pointers solve sorted 2-sum problem
if N == 2:
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:
for i in range(l, r + 1):
if i == l or (i > l and nums[i - 1] != nums[i]):
findNsum(i + 1, r, target - nums[i], N - 1, result + [nums[i]], results)
nums.sort()
results = []
findNsum(0, len(nums) - 1, target, 4, [], results)
return results