leetcode 字符串匹配Implement strStr()

mplement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

判断字符串needle是否在字符串haystack中,若在在返回第一次出现的下标。

class Solution {
public:
int strStr(string haystack, string needle) {
	int l=needle.length();
	int l1=haystack.length();
	int i;
	for(i=0;i<l1-l+1;i++)
   {	string haystack1;
	haystack1.assign(haystack.substr(i,l));
	cout<<haystack1<<endl;
	if(haystack1.compare(needle)==0)return i;
	 }
return -1;}
};


你可能感兴趣的:(leetcode 字符串匹配Implement strStr())