letcode_实现strStr()

题目

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (0开始)。如果不存在,则返回  -1。

示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1

来源:力扣(LeetCode)

解法一:(窗口移动)

思路
 子串逐一比较:沿着子字符串的长度逐步移动滑动窗口,将窗口里的字符串和needle比较
代码
    /**
     * 解法一:
     * 子串逐一比较:沿着子字符串的长度逐步移动滑动窗口,将窗口里的字符串和needle比较
     * @param haystack
     * @param needle
     * @return
     */
    public static int strStr_1(String haystack, String needle) {
        if(needle.equals("")) return 0;
        int h_length = haystack.length();
        int n_length = needle.length();
        //h_length-n_length --> 可以滑动的最大长度
        for (int i = 0; i < (h_length-n_length)+1; i++) {
            if(haystack.substring(i,i+n_length).equals(needle)){
                return i;
            }
        }
        return -1;
    }
结果

在这里插入图片描述

解法二:辅助下标List(和字串第一个字符串相等的下标)

思路
 上一个方法的中会将haystack中所有和needle一样长度的字串比较,实际上我们只需要第一个字符相同才要比较
(1)用一个辅助的List用来存放,needle的第一个字符在haystack中位置下标
(2)遍历List中存的下标,在haystack截取字符串和needle比较
代码
 /**
     * 解法二:辅助下标List(和字串第一个字符串相等的下标)
     * 上一个方法的中会将haystack中所有和needle一样长度的字串比较,实际上我们只需要第一个字符相同才要比较
     * 思路:
     *(1)用一个辅助的List用来存放,needle的第一个字符在haystack中位置下标
     *(2)遍历List中存的下标,在haystack截取字符串和needle比较
     * @param haystack
     * @param needle
     * @return
     */
    public static int strStr_2(String haystack, String needle){
        if(needle.equals("")) return 0;
        int h_length = haystack.length();
        int n_lengyh = needle.length();
        //用list存和needle第一个字符相等的下标
        List<Integer> s_index = new ArrayList();
        for (int i = 0; i < (h_length-n_lengyh+1); i++) {
            if(haystack.charAt(i) == needle.charAt(0)){
                s_index.add(i);
            }
        }
        //遍历开始的下标,截取字符串与needle比较
        for (int i = 0; i < s_index.size(); i++) {
            if(haystack.substring(s_index.get(i),(s_index.get(i)+n_lengyh)).equals(needle)){
                return s_index.get(i);
            }
        }
        return -1;
    }
结果

在这里插入图片描述

解法三:双指针
思路
 (1)移动 ph 指针,直到 ph 所指向位置的字符与 needle 字符串第一个字符相等
 (2)通过 ph,pn,cur_len 计算匹配长度
 (3)如果完全匹配(即 cur_len == n_len),返回匹配子串的起始坐标(即 ph - n_len)
 (4)如果不完全匹配,回溯。使 ph = ph - cur_len + 1, pn = 0, cur_len = 0

在这里插入图片描述

代码
    /**
     * 解法三:(双指针)
     *思路:
     * (1)移动 ph 指针,直到 ph 所指向位置的字符与 needle 字符串第一个字符相等
     * (2)通过 ph,pn,cur_len 计算匹配长度
     * (3)如果完全匹配(即 cur_len == n_len),返回匹配子串的起始坐标(即 ph - n_len)
     * (4)如果不完全匹配,回溯。使 ph = ph - cur_len + 1, pn = 0, cur_len = 0
     * @param haystack
     * @param needle
     * @return
     */
    public static int strStr_3(String haystack, String needle){
        if(needle.equals("")){
            return 0;
        }
        int h_len = haystack.length();
        int n_len = needle.length();
        //定义指向haystack的指针
        int ph = 0;
        while(ph < (h_len-n_len+1)){
            //如果ph对应的字符串和needle的第一个字符串不相等,则向后移动
            while(ph < (h_len-n_len+1) && haystack.charAt(ph) != needle.charAt(0)) ++ph;

            //定义指向needle的指针
            int pn = 0;
            int cur_len = 0;

            while (ph < h_len && pn < n_len && haystack.charAt(ph) == needle.charAt(pn)){
                //如果ph指向的字符串和pn都相等,一起向后移,同时当前长度增加
                ++cur_len;
                ++pn;
                ++ph;
            }

            //如果当前的长度和needle的长度一直,则找到第一次出现匹配的位置
            if(cur_len == n_len) return ph - n_len;

            //ph 和 pn 在匹配的时候,发现有不匹配的,则ph返回到初始位置加1
            ph = ph - cur_len + 1;
        }
        return -1;
    }
结果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值