LeetCode题解之 Implement strStr()

时间:2022-06-22 10:43:55

1、题目描述

LeetCode题解之 Implement strStr()

2.题目分析

字符串操作,注意边界条件即可。

3.代码

  int strStr(string haystack, string needle) {
int n = needle.size();
int res = -;
if( needle.size() == )
return ;
for(string::iterator it = haystack.begin() ; it != haystack.end(); ++it){
if((*it) == needle[]){
bool isEqual = true;
bool isEnd = false;
for(int i = ; i < n; i++){
if( it + i == haystack.end() ){
isEnd = true;
break;
}
if( needle[i] != *(it+i)){
isEqual = false;
break;
}
}
if( isEnd == true )
break;
if( isEqual == true){
res = it-haystack.begin();
break;
} }
}
return res; }