php双指针算法问题,Leetcode题解 - 双指针求n数之和

1. 两数之和"""

双指针,题目需要返回下标,所以记录一个数字对应的下标

"""

class Solution:

def twoSum(self, nums: List[int], target: int) -> List[int]:

r = [[ind, num] for ind, num in enumerate(nums)]

r = sorted(r, key=lambda x: x[1])

head = 0

tail = len(r) - 1

while head < tail:

s = r[head][1] + r[tail][1]

if s == target:

return [r[head][0], r[tail][0]]

elif s > target:

tail -= 1

# 跳过重复值,去重的规则都一样(下面的也是这样)

while head < tail and r[tail][1] == r[tail+1][1]:

tail -= 1

else:

head += 1

while head < tail and r[head][1] == r[head-1][1]:

head += 1

15. 三数之和"""

双指针,所以先固定一个数字,用双指针来找到另外两个数字。注意记得剪枝,否则一定会超时的。(被超时操控的恐惧...

"""

class Solution:

def threeSum(self, nums: List[int]) -> List[List[int]]:

nums.sort()

vis = set()

res = []

for i in range(len(nums)):

if nums[i] in vis:

continue

if nums[i] > 0:

break

vis.add(nums[i])

head = i + 1

tail = len(nums) - 1

while head < tail and head < len(nums) and tail < len(nums):

s = nums[i] + nums[head] + nums[tail]

if not s:

res.append([nums[i], nums[head], nums[tail]])

head += 1

tail -= 1

# 跳过重复元素

while head < tail and nums[head] == nums[head - 1]:

head += 1

while head < tail and nums[tail] == nums[tail + 1]:

tail -= 1

if s > 0:

tail -= 1

while head < tail and nums[tail] == nums[tail + 1]:

tail -= 1

if s < 0:

head += 1

while head < tail and nums[head] == nums[head - 1]:

head += 1

return res

18. 四数之和"""

固定两个,双指针找另外两个

"""

class Solution:

def fourSum(self, nums, target):

nums.sort()

res = []

for i in range(len(nums)):

for j in range(i+1, len(nums)):

head = j + 1

tail = len(nums) - 1

while head < tail:

s = nums[i] + nums[j] + nums[head] + nums[tail]

if s == target:

res.append([nums[i], nums[j], nums[head], nums[tail]])

head += 1

tail -= 1

while head < tail and nums[head] == nums[head - 1]:

head += 1

while head < tail and nums[tail] == nums[tail + 1]:

tail -= 1

elif s > target:

tail -= 1

while head < tail and nums[tail] == nums[tail + 1]:

tail -= 1

else:

head += 1

while head < tail and nums[head] == nums[head - 1]:

head += 1

return list(set([tuple(i) for i in res]))

总结

可以看到,无论是2、3or4,都是固定除了双指针之外的元素,再用双指针去找剩下的元素,代码几乎没有改变,切记要记得剪枝。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值