zoukankan      html  css  js  c++  java
  • 【LeetCode】28. Implement strStr()

    Difficulty:easy

     More:【目录】LeetCode Java实现

    Description

    https://leetcode.com/problems/implement-strstr/

    Implement strStr().

    Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

    Example 1:

    Input: haystack = "hello", needle = "ll"
    Output: 2
    

    Example 2:

    Input: haystack = "aaaaa", needle = "bba"
    Output: -1
    

    Clarification:

    What should we return when needle is an empty string? This is a great question to ask during an interview.

    For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

    Intuition

    1.Brute-force solution: Use two pointers

    2. KMP algorithm 

    Solution

    Brute force solution:

        public int strStr(String haystack, String needle) {
            if(haystack==null || needle==null)
                return -1;
            for(int i=0;;i++){
                for(int j=0;;j++){
                    if(j==needle.length())  return i;
                    if(i+j==haystack.length())  return -1;
                    if(haystack.charAt(i+j)!=needle.charAt(j))    break;
                }
            }
        }
    
        public int strStr(String haystack, String needle) {
            if(needle.isEmpty())
                return 0;
            int i=0,j=0;
            while(i<haystack.length()){
                if(haystack.charAt(i)==needle.charAt(j)){
                    i++;
                    j++;
                    if(j==needle.length())
                        return i-j;
                    if(i==haystack.length())
                        return -1;
                }else{
                    i=i-j+1;
                    j=0;
                }
            }
            return -1;
        }
    

      

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

      

    KMP:

        public int strStr(String haystack, String needle) {
            if(needle.isEmpty())
                return 0;
            int[] next = getNext(needle);
            int i=0, j=0;
            while(i<haystack.length()){
                if(j==-1 || haystack.charAt(i)==needle.charAt(j)){
                    i++;
                    j++;
                    if(j==needle.length())
                        return i-j;
                }else{
                    j=next[j];
                }
            }
            return -1;
        }
        
       //next[i]数组:
        //  1. i=0时,next[i]=-1;
        //  2. 前后缀相等长度为n时, next[i]=n;(可改进)
        //  3. 其余:next[i]=0;
        private static int[] getNext(String str) {
            if (str == null || str.isEmpty())
                return null;
            int[] next = new int[str.length()];
            next[0] = -1;
            int i = 0, j = -1;  //i为后缀的位置,j为前缀位置
            while (i < str.length() - 1) {  //此处范围注意
                if (j == -1 || str.charAt(i) == str.charAt(j)) {
                    i++;
                    j++;
                    //next[i] = j;
                    next[i] = str.charAt(i) == str.charAt(j) ? next[j] : j; //前后缀相等长度为j
                } else {
                    j = next[j];
                }
            }
            return next;
        }
    

      

    Complexity

    Brute Force:

    Time complexity : 

    Assume that n = length of haystack and m = length of needle, then the runtime complexity is O(nm).

    Space complexity :  O(1)

     

    KMP:

    Time complexity : O(n)

    Space complexity: O(m)

    What I've learned

    1. It is a great question to ask "What should we return when needle is an empty string?"  during an interview.

     More:【目录】LeetCode Java实现

  • 相关阅读:
    [VB]用API打开浏览文件夹对话框,选择文件夹
    C# 16进制与字符串、字节数组之间的转换
    DIV未知高度的垂直居中代码
    Webbrowser控件判断网页加载完毕的简单方法
    一些控制鼠标的例子!
    Kernel device tree: simplebus
    Display Serial Interface (From WIKI)
    消费提示:常见 处理器/显卡 性能排名网站 汇总
    HDMI notes From HDMI wiki
    Linux graphics stack 理解
  • 原文地址:https://www.cnblogs.com/yongh/p/10018767.html
Copyright © 2011-2022 走看看