您的位置:首页 > 其它

Leetcode 28. Implement strStr()

2017-01-29 08:18 246 查看
// naive O(n^2) solution
public class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) return 0;
if (haystack.length() < needle.length()) return -1;
// index of i is between [0, haystack-needle]
for (int i=0; i<=haystack.length()-needle.length(); i++) {
int tmp = i;
int j;
for (j=0; j<needle.length(); j++) {
if (haystack.charAt(tmp) != needle.charAt(j))
break;
tmp++;
}
if (j == needle.length()) return i;
}
return -1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: