Implement strStr()

来源:互联网 发布:从事java开发特别累吗? 编辑:程序博客网 时间:2024/06/07 12:34

没有考虑针对大海大的情况,哈哈。

#include<iostream>#include<string>using namespace std;class Solution {public:    int strStr(string haystack, string needle) {if(haystack.size() < needle.size())return -1;string::iterator iter1,iterTmp,iter2;for(iter1 = haystack.begin(); iter1 != haystack.end() - needle.size() + 1; iter1++) {iterTmp = iter1;for(iter2 = needle.begin(); iter2 != needle.end(); ) {if(*iterTmp != *iter2)break;iterTmp++;iter2++;}if(needle.end() == iter2  )break;}int index;if(iter1 == haystack.end() - needle.size() + 1)index = -1;elseindex = iter1 - haystack.begin();return index;    }};int main() {string haystack = "abcdefgoythonhijklmn";string needle = "python";Solution solution;cout << solution.strStr(haystack, needle) << endl;getchar();return 1;}

0 0
原创粉丝点击