Day 1.25
实现strStr()
题目
代码
class Solution {
public int strStr(String haystack, String needle) {
int lenh = haystack.length();
int lenn = needle.length();
if(lenn==0)
return 0;
if(lenn>lenh)
return -1;
char[] s = haystack.toCharArray();
char[] n = needle.toCharArray();
//外循环次数为haystack长度减去needle的长度
for (int i = 0; i < lenh-lenn+1; i++) {
boolean f = true;
for (int j = 0,star = i; j < lenn; j++,star++) {
if(s[star]!=n[j]) {
f = false;
break;
}
}
if(f)
return i;
}
return -1;
}
}