您的位置:首页 > 其它

LeetCode 125 Implement strStr()

2014-10-31 20:17 477 查看
Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

分析:

从0开始每次截取needle长度的字串来匹配,如果相等则找到。

public class Solution {
public String strStr(String haystack, String needle) {

int len = needle.length();
for(int i=0; i<=haystack.length()-len; i++){
String sub = haystack.substring(i,i+len);
if(sub.equals(needle)){
return haystack.substring(i);
}
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息