mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
添加0018.四数之和双指针法python3版本
This commit is contained in:
@ -167,7 +167,33 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
|
# 双指针法
|
||||||
|
class Solution:
|
||||||
|
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
|
||||||
|
|
||||||
|
nums.sort()
|
||||||
|
n = len(nums)
|
||||||
|
res = []
|
||||||
|
for i in range(n):
|
||||||
|
if i > 0 and nums[i] == nums[i - 1]: continue
|
||||||
|
for k in range(i+1, n):
|
||||||
|
if k > i + 1 and nums[k] == nums[k-1]: continue
|
||||||
|
p = k + 1
|
||||||
|
q = n - 1
|
||||||
|
|
||||||
|
while p < q:
|
||||||
|
if nums[i] + nums[k] + nums[p] + nums[q] > target: q -= 1
|
||||||
|
elif nums[i] + nums[k] + nums[p] + nums[q] < target: p += 1
|
||||||
|
else:
|
||||||
|
res.append([nums[i], nums[k], nums[p], nums[q]])
|
||||||
|
while p < q and nums[p] == nums[p + 1]: p += 1
|
||||||
|
while p < q and nums[q] == nums[q - 1]: q -= 1
|
||||||
|
p += 1
|
||||||
|
q -= 1
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
```python
|
||||||
|
# 哈希表法
|
||||||
class Solution(object):
|
class Solution(object):
|
||||||
def fourSum(self, nums, target):
|
def fourSum(self, nums, target):
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user