Leetcode P28 Java双指针解决

Leetcode P28 Java双指针解决

Ideas

执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户

内存消耗:39.1 MB, 在所有 Java 提交中击败了93.42%的用户

首先我们获取下每个字符串的长度,然后判断一下特殊情况

        int m = needle.length();
        int n = haystack.length();
        //特殊情况
        if (m == 0 || n < m){
            return -1;
        }

接下来创建2个指针负责移动

        int pointA = 0; //负责ahystack的指针
        int pointB = 0; //负责needle的指针

如果haystack的字符串包含needle,那么必须保证在[0,n -m + 1]区间内存在,如果不再这个区间内存在直接返回-1

也就是pointA必须在这个区间内找到首字母和needle一致的,并且后续内容也等于needle

关于为什么是n-m+1是因为

假如

​ haystack is abcde length is 5

​ needle is ab length is 2

通过5-2 =3 我们得出我们从c坐标开始,但是我们的极限在 4,也就是说通过这个公式我们找到我们指针最后的一个开始位置

       while(pointA < n - m +1){
            //找到首字母相同的数据
            while (pointA < n - m +1 && haystack.charAt(pointA) != needle.charAt(pointB)){
                pointA++;
            }
            //查看是否找到
            if (pointA >= n - m + 1){
                return -1;
            }
            //指针下移
            pointA++;
            pointB++;
            /**
             * 查看后续字符是否一致
             */
            while (pointA < n && pointB < m  && haystack.charAt(pointA) == needle.charAt(pointB)){
                pointA++;
                pointB++;
            }
            //查看是否找到
            if (pointB == m){

                return pointA - pointB;
            }
            //如果没有找到, 比如说pointA是从0开始判断的,那么就让pointA从1开始判断
            else{
                pointA -= pointB-1;
                pointB = 0;
            }
        }

code

class Solution {
    public int strStr(String haystack, String needle) {
        int m = needle.length();
        int n = haystack.length();
        //特殊情况
        if (m == 0 || n < m){
            return -1;
        }
        int pointA = 0; //负责ahystack的指针
        int pointB = 0; //负责needle的指针
        /**
         * 如果haystack的字符串包含needle,那么必须保证在[0,n -m + 1]区间内存在,如果不再这个区间内存在直接返回-1
         * 也就是pointA必须在这个区间内找到首字母和needle一致的,并且后续内容也等于needle
         * 关于为什么是n-m+1是因为
         * 假如
         *      haystack is abcde       length is 5
         *      needle   is ab          length is 2
         *      通过5-2 =3 我们得出我们从c坐标开始,但是我们的极限在 4,也就是说通过这个公式我们找到我们指针最后的一个开始位置
         */
        while(pointA < n - m +1){
            //找到首字母相同的数据
            while (pointA < n - m +1 && haystack.charAt(pointA) != needle.charAt(pointB)){
                pointA++;
            }
            //查看是否找到
            if (pointA >= n - m + 1){
                return -1;
            }
            //指针下移
            pointA++;
            pointB++;
            /**
             * 查看后续字符是否一致
             */
            while (pointA < n && pointB < m  && haystack.charAt(pointA) == needle.charAt(pointB)){
                pointA++;
                pointB++;
            }
            //查看是否找到
            if (pointB == m){

                return pointA - pointB;
            }
            //如果没有找到, 比如说pointA是从0开始判断的,那么就让pointA从1开始判断
            else{
                pointA -= pointB-1;
                pointB = 0;
            }
        }
        return -1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇塞大嘴好帅(DaZuiZui)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值