您的位置:首页 > 其它

Implement strStr()

2015-07-03 18:51 429 查看

称号



Implement strStr().

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



方法


仅仅须要遍历一遍就可以。

public String strStr(String haystack, String needle) {

int lenH = haystack.length();
int lenN = needle.length();
if(lenH < lenN ){
return null;
}

for(int i = 0; i < lenH - lenN + 1; i ++){

int j = 0;
while(j < lenN){
if(haystack.charAt(i + j) != needle.charAt(j)){
break;
}
j ++;
}
if(j == lenN){
return haystack.substring(i, lenH);
}
}
return null;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: