zoukankan      html  css  js  c++  java
  • 28. 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().

    AC code:

    this code is low effective.

    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int len1 = haystack.length();
            int len2 = needle.length();
            if (len2 == 0) {
                return 0;
            }
            for (int i = 0; i < len1; ++i) {
                int index = i;
                int j = 0;
                while (haystack[index] == needle[j] && index < len1 && j < len2) {
                    index++;
                    j++;
                }
                if (j == len2) {
                    return i;
                }
            }
            return -1;
        }
    };
    

    Runtime: 812 ms, faster than 3.41% of C++ online submissions for Implement strStr().

    Only have a little change, but the effective have a big exaltation.

    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int len1 = haystack.length();
            int len2 = needle.length();
            if (len2 == 0) {
                return 0;
            }
         // i < len1 - len2 for (int i = 0; i < len1-len2+1; ++i) { int index = i; int j = 0; while (haystack[index] == needle[j] && index < len1 && j < len2) { index++; j++; } if (j == len2) { return i; } } return -1; } };

    Runtime: 8 ms, faster than 37.69% of C++ online submissions for Implement strStr().

    But this way is a brute-force algorithm, too. may be this question want you to use brute-force.

    The high effective algorithm is to use KMP algorithm.

    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int m = haystack.length(), n = needle.length();
            if (!n) {
                return 0;
            }
            vector<int> lps = kmpProcess(needle);
            for (int i = 0, j = 0; i < m; ) {
                if (haystack[i] == needle[j]) { 
                    i++;
                    j++;
                }
                if (j == n) {
                    return i - j;
                }
                if ((i < m) && (haystack[i] != needle[j])) {
                    if (j) {
                        j = lps[j - 1];
                    }
                    else {
                        i++;
                    }
                }
            }
            return -1;
        }
    private:
        vector<int> kmpProcess(string& needle) {
            int n = needle.length();
            vector<int> lps(n, 0);
            for (int i = 1, len = 0; i < n; ) {
                if (needle[i] == needle[len]) {
                    lps[i++] = ++len;
                } else if (len) {
                    len = lps[len - 1];
                } else {
                    lps[i++] = 0;
                }
            }
            return lps;
        }
    };
    

    Runtime: 8 ms, faster than 37.69% of C++ online submissions for Implement strStr().

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    java怎么导入一个项目到eclipse
    JDK安装与环境变量配置(链接by网络)
    配置安装ecplise跑项目
    电脑缺少**.dll文件
    Microsoft-Office-Professional-Plus-2007
    win7如何恢复以前的ie版本
    maven安装及maven项目导入流程(网络链接)
    LoadRunner录制Web协议的脚本 (by网络)
    spring中jdbc.properties用法
    linux 安装 mysql
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9748607.html
Copyright © 2011-2022 走看看