【LeetCode 28】实现strStr()(Java)

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

【参考示例】
示例 1:

  • 输入:haystack = “hello”, needle = “ll”
  • 输出:2

示例 2:

  • 输入:haystack = “aaaaa”, needle = “bba”
  • 输出:-1

【说明】
当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

【解题思路】

方法一:暴力法

分别循环两个字符串,对比两个字符串是否有公共的部分,循环不必从头到尾,对于原字符串haystack来说,循环到haystack-needle的位置就可以结束了。

class Solution {
    
    
    public int strStr(String haystack, String needle) {
    
    
        int m = haystack.length(), n = needle.length();
        if (n == 0) return 0;
        for (int i = 0; i <= m - n; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                if (haystack.charAt(i + j) != needle.charAt(j)) break;
                if (j == n - 1) return i;
            }
        }
        return -1;
    }
}

在这里插入图片描述

方法二:采用官方提供的API解决
class Solution {
    
    
  public int strStr(String haystack, String needle) {
    
    
    int m = haystack.length(), n = needle.length();

    for (int start = 0; start < m - n + 1; ++start) {
    
    
      if (haystack.substring(start, start + n).equals(needle)) {
    
    
        return start;
      }
    }
    return -1;
  }
}

在这里插入图片描述

方法三:双指针法
class Solution {
    
    
    public int strStr(String haystack, String needle) {
    
    
        if (needle.length()==0){
    
    
            return 0;
        }
        for (int h = 0,n = 0; h < haystack.length() && n<needle.length();){
    
    
            if (haystack.charAt(h) != needle.charAt(n) && n==0){
    
    
                h++;
                continue;
            }
            if (haystack.charAt(h) != needle.charAt(n) && n!=0){
    
    
                h-=n-1;
                n = 0;
                continue;
            }
            if (n == needle.length() - 1)
                return h-needle.length()+1;
            h++;
            n++;
        }
        return -1;
    }
}

在这里插入图片描述

方法四:KMP算法

这个算法理解有点难度,可以看看这个视频(一遍不理解就多看几遍)
B站:KMP算法详细解释

class Solution {
    
    
    public int strStr(String haystack, String needle) {
    
    
        int h = haystack.length();
        int n = needle.length();
        // 判断是否为空
        if(n==0){
    
    
            return 0;
        }
        if(h<n){
    
    
            return -1;
        }

        // next数组存储公共前后缀needle
        int []next = new int[n];
        int j = 0;  //前缀指针
        for(int i = 1; i < n; i++){
    
       //后缀指针
            while (j - 1 >= 0 && needle.charAt(j) != needle.charAt(i)){
    
    
                j = next[j - 1];    //最前缀指针跳动
            }
            if(needle.charAt(j) == needle.charAt(i)){
    
    
                next[i] = j + 1;
                j++;
            }
        }
        
        // KMP算法比较
        int k = 0;
        for(int i = 0; i < h; i++){
    
    
            //当字符匹配时,根据nexts数组,回退子串指针。特别注意是while循环
            while (k - 1 >= 0 && haystack.charAt(i) != needle.charAt(k)){
    
    
                k = next[k - 1];//回退
            }
            if(haystack.charAt(i) == needle.charAt(k)){
    
    
                k++;
                if(k == n){
    
    //当子串全部匹配成功时,返回结果
                    return i - n + 1;
                }
            }

        }
        return -1;
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42908549/article/details/112674468
今日推荐