美文网首页
28. Implement strStr()

28. Implement strStr()

作者: namelessEcho | 来源:发表于2017-09-23 21:49 被阅读0次

因为是easy所以不要求实现KMP,暴力搜索就好了。

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

相关文章

网友评论

      本文标题:28. Implement strStr()

      本文链接:https://www.haomeiwen.com/subject/gurhextx.html