leetcode:28. Implement strStr()

来源:互联网 发布:加拿大医学院申请知乎 编辑:程序博客网 时间:2024/05/17 01:44

描述

Implement strStr().

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

思路

这一题就是找子串,思路有很多:
思路一:暴力破解,从haystack的0位置遍历。
思路二:利用string自带的find方法
思路三:KMP算法
上诉三个思路,第一个可能时间会超时,第三个难理解编写有难度,我投机取巧使用思路二。

代码

class Solution {public:    int strStr(string haystack, string needle) {        size_t position;          position = haystack.find(needle);          if (position != string::npos)              return position;          else              return -1;    }};

结果

这里写图片描述

他山之玉

C++ O(n) solutioin

class Solution {public:    int strStr(string haystack, string needle) {        string s=" "+needle+"#"+haystack;        if (needle.size()==0) {            return 0;        }        if (haystack.size()==0) {            return -1;        }        vector<int> p(s.size()+1,0);        int t=0;        for (int i=2; i<=s.size(); i++) {            while (s[t+1]!=s[i] && t>0) {                t=p[t];            }            if (s[t+1]==s[i]) {                t++;            }            p[i] = t;            if (p[i]==needle.size()) {                return i-(2*needle.size())-1;            }        }        return -1;    }};

这个是使用kmp算法的,不过这个算法并没有获得next数组,而是在遍历的时候记录。

Java O(n) solution

public class Solution {    public int strStr(String haystack, String needle) {        int haystacklength = haystack.length();        int needlelength = needle.length();        if(needlelength > haystacklength) return -1;        if(needlelength ==0 ) return 0;        int needlehashcode = needle.hashCode();        for(int i=0;i<=haystacklength-needlelength;i++){            if(haystack.substring(i,i+needlelength).hashCode() == needlehashcode) return i;        }        return -1;    }}

这个使用暴力破解,不过在对比子串的时候,不是使用子串中字符一一对比来做的,而是将i位置后needlelength长度字符串与neddle字符串进行对比。如果经过优化,很有可能比KMP还快
python O(n) solution

class Solution(object):    def strStr(self, haystack, needle):        if not needle: return 0        if not haystack: return -1        next_arr = self.create_next(needle)        i = j = 0        while i < len(haystack) and j < len(needle):            if haystack[i] == needle[j]:                # Matched, so return the haystack's match start index.                if j == len(needle) - 1:                    return i - len(needle) + 1                i, j = i + 1, j + 1            else:                # Slide pattern over.                if j: j = next_arr[j-1]                 else: i += 1        return -1    # Build next jump table.     def create_next(self, pattern):        next_arr = [0] * len(pattern)        pre_i, suf_i = 0, 1        while suf_i < len(pattern):            # Found prefix-suffix match.             if pattern[pre_i] == pattern[suf_i]:                next_arr[suf_i] = pre_i + 1                pre_i, suf_i = pre_i + 1, suf_i + 1            else:                if pre_i:                    pre_i = next_arr[pre_i-1]                else:                    next_arr[suf_i] = 0                    suf_i += 1        return next_arr

正宗KMP算法

0 0