标签: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)
标签:style blog color io ar for sp div c
原文地址:http://www.cnblogs.com/leetcode/p/4005042.html