Question:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Solution:
public class Solution {
public int strStr(String haystack, String needle) {
int m=haystack.length();
int n=needle.length();
for(int i=0;i<=m-n;i++)
{
int j=0;
for(j=0;j<=n-1;j++)
{
if(haystack.charAt(i+j)!=needle.charAt(j))
break;
}
if(j==n)
return i;
}
return -1;
}
}
还有o(m+n)的solution