leetcode(15)双指针系列题目

287. 寻找重复数

快慢指针,类似于找环的入口

从理论上讲,数组中如果有重复的数,那么就会产生多对一的映射,这样,形成的链表就一定会有环路了,

综上
1.数组中有一个重复的整数 <> 链表中存在环
2.找到数组中的重复整数 <
> 找到链表的环入口

至此,问题转换为 142 题。

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        fast, slow = 0, 0
        while True:
            fast = nums[nums[fast]]
            slow = nums[slow]
            if fast == slow:
                break
        fast = 0
        while fast != slow:
            fast = nums[fast]
            slow = nums[slow]
        return fast

202. 快乐数

快慢指针,如果是快乐数,low=fast=1;如果不是快乐数,low=fast !=1,循环了。

class Solution:
    def isHappy(self, n: int) -> bool:
        n = str(n)
        slow = n
        fast = str(sum(int(i)**2 for i in n))
        while slow != fast:
            slow = str(sum(int(i)**2 for i in slow))
            fast = str(sum(int(i)**2 for i in fast))
            fast = str(sum(int(i)**2 for i in fast))
        return fast == '1'

15. 三数之和

首先将数组排序,然后用一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。

依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i] b = nums[left] c = nums[right]。

  • 如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。
  • 如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        n = len(nums)
        res = []
        nums.sort()
        for i in range(n):
            if nums[i] > 0:
                break
            if i >= 1 and nums[i] == nums[i - 1]:
                continue
            left, right = i+1, n-1
            while left < right:
                sumCur = nums[i] + nums[left] + nums[right]
                if sumCur < 0:
                    left += 1
                elif sumCur > 0:
                    right -= 1
                else:
                    res.append([nums[i], nums[left], nums[right]])
                    while left != right and nums[left] == nums[left + 1]:
                        left += 1
                    while left != right and nums[right] == nums[right - 1]:
                        right -= 1
                    left += 1
                    right -= 1
        return res

18. 四数之和

注意要先排序,用两层for循环

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        res = []
        n = len(nums)
        nums.sort()  # 注意要先排序
        for a in range(n):
            if a > 0 and nums[a] == nums[a -1]:
                continue
            for b in range(a + 1, n):
                if b > a + 1 and nums[b] == nums[b -1]:
                    continue
                c, d = b + 1, n - 1
                while c < d:
                    sum_ = nums[a] + nums[b] + nums[c] + nums[d]
                    if sum_ < target:
                        c += 1
                    elif sum_ > target:
                        d -= 1
                    else:
                        res.append([nums[a],nums[b],nums[c],nums[d]])
                        while c != d and nums[c] == nums[c + 1]:
                            c += 1
                        while c != d and nums[d] == nums[d - 1]:
                            d -= 1
                        c += 1
                        d -= 1
        return res

344. 反转字符串

class Solution:
    def reverseString(self, s: List[str]) -> None:
        n = len(s)
        left, right = 0, n - 1
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1

1662. 检查两个字符串数组是否相等

调用api的写法,时间复杂度是O(m)

class Solution:
    def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
        return ''.join(word1) == ''.join(word2)

直接比较,时间复杂度是O(1)

class Solution:
    def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
        len1, len2 = len(word1), len(word2)
        i, j, m, n = 0, 0, 0, 0
        while m < len1 and n < len2:
            if word1[m][i] != word2[n][j]:
                return False
            i, j = i + 1, j + 1
            if i == len(word1[m]):
                i, m = 0, m + 1
            if j == len(word2[n]):
                j, n = 0, n + 1
        return m == len1 and n == len2

75. 颜色分类

用两次快慢指针

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        r = w = 0
        while r < len(nums):
            if nums[r] == 0:
                nums[w], nums[r] = nums[r], nums[w]
                w += 1
            r += 1
        b = w 
        while b < len(nums):
            if nums[b] == 1:
                nums[b], nums[w] = nums[w], nums[b]
                w += 1
            b += 1


posted @ 2022-05-17 20:45  YTT77  阅读(50)  评论(0编辑  收藏  举报