码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode题解 - 双指针求n数之和

时间:2019-12-22 16:24:02      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:get   add   int   alt   img   sort   ted   总结   tail   

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,都是固定除了双指针之外的元素,再用双指针去找剩下的元素,代码几乎没有改变,切记要记得剪枝。

Leetcode题解 - 双指针求n数之和

标签:get   add   int   alt   img   sort   ted   总结   tail   

原文地址:https://www.cnblogs.com/NFii/p/12079105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!