美文网首页
02-13:leetcode重刷1之双指针

02-13:leetcode重刷1之双指针

作者: 是黄小胖呀 | 来源:发表于2021-02-13 13:51 被阅读0次

双指针的思路:

一般在排好序的数组中寻找某种pair

主要有两种吧

一种是同时从前面开始

另一种是前后开始

1、两数之和为某值,167. 两数之和 II - 输入有序数组

(1)前后指针

class Solution:

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

        i=0

        k=len(numbers)

        j=k-1

        while i<j:

            if numbers[i]+numbers[j]>target:

                  j=j-1

            elif numbers[i]+numbers[j]<target:

                  i=i+1

            else:

                break

        result=[]

        result.append(i+1)

        result.append(j+1)

        return result

2、合并的排序数组,面试题 10.01. 合并排序的数组

(2)同向的两个指针

从头开始:

class Solution:

    def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:

        """

        Do not return anything, modify A in-place instead.

        """

        i=0

        j=0

        res=[]

        while  i<m  or  j<n:

            if i==m:

                res.append(B[j])

                j=j+1

            elif j==n:

                res.append(A[i])

                i=i+1

            elif A[i]<B[j]:

                 res.append(A[i])

                 i=i+1

            else:

                 res.append(B[j])

                 j=j+1

        A[:]=res

        return

从末尾开始

class Solution:

    def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:

        """

        Do not return anything, modify A in-place instead.

        """

        i=m-1

        j=n-1

        t=m+n-1

        while  i>=0  or  j>=0:

            if i==-1:

                A[t]=B[j]

                j=j-1

            elif j==-1:

                A[t]=A[i]

                i=i-1

            elif A[i]>B[j]:

                 A[t]=A[i]

                 i=i-1

            else:

                 A[t]=B[j]

                 j=j-1

            t=t-1

        return

3、子序列问题

双指针

class Solution:

    def isSubsequence(self, s: str, t: str) -> bool:   

        n, m = len(s), len(t)

        i = j = 0

        while i < n and j < m:

            if s[i] == t[j]:

                i += 1

            j += 1

        return i == n

相关文章

  • 02-13:leetcode重刷1之双指针

    双指针的思路: 一般在排好序的数组中寻找某种pair 主要有两种吧 一种是同时从前面开始 另一种是前后开始 1、两...

  • 02-13:leetcode重刷2之链表反转

    1、链表反转 2、反转二叉树 3、合并二叉树 4、对称二叉树 1、反转链表 classSolution: defr...

  • 02-13:leetcode重刷7之动态规划

    动态规划 动态规划的重点是:状态转移方程 1、判断子序列 leetcode392. 判断子序列[https://l...

  • LeetCode 数组专题 4:双索引技术之一:对撞指针

    在 LeetCode 上,专门有一个标签,名为“双指针”,有数组中的“双指针”,也有单链表中的“双指针”。 例题1...

  • 02-13:leetcode重刷3之树的遍历

    二叉树的遍历: 前序、中序、后序遍历 二叉搜索树 小左,大右,所以二叉搜索树的中序遍历是递增序列 (1)深度优先遍...

  • LeetCode之双指针法

    双指针是一种思想或一种技巧并不是特别具体的算法。具体就是用两个变量动态存储两个结点,来方便我们进行一些操作。通常用...

  • 大厂算法面试之leetcode精讲7.双指针

    大厂算法面试之leetcode精讲7.双指针 视频教程(高效学习):点击学习[https://xiaochen10...

  • leetcode之重新排列数组

    序 本文主要研究一下leetcode之重新排列数组 题目 题解 小结 这里使用双指针,两个指针都从0开始,一个每次...

  • leetcode141-环形链表

    题目:见leetcode141 解答: 官方解答 解法一:双指针 时间复杂度:n,空间复杂度:1//快慢两个指针 ...

  • 双指针

    双指针问题总结 双指针经典问题 twoSum (有序数组) 字符串翻转 先看一个例子: leetcode 345....

网友评论

      本文标题:02-13:leetcode重刷1之双指针

      本文链接:https://www.haomeiwen.com/subject/xmmsxltx.html