码迷,mamicode.com
首页 > 其他好文 > 详细

strstr

时间:2014-10-03 18:20:54      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   sp   div   c   

N(n*m)的时间复杂度

public class Solution {
public String strStr(String haystack, String needle) {
 
    int nLen = needle.length();
    int hLen = haystack.length();
    
    if(nLen==hLen && nLen==0)
        return "";
    if(nLen == 0)
        return haystack;
    for(int i=0; i<= hLen-nLen; i++){
        int j = 0;
        for( ; j<nLen;j++){
            if(needle.charAt(j)!=haystack.charAt(i+j))break;
        }
        if(j==nLen)
            return haystack.substring(i);
    }
    
    return null;
 
   
}

}

 

kmp:  N(m+n)

strstr

标签:style   blog   color   io   ar   for   sp   div   c   

原文地址:http://www.cnblogs.com/leetcode/p/4005042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!