strstr
方法一(第一时间想到):
int strstr(string haystack, string needle)
{
if (needle.empty())
return 0;
int flag = 0;
int index = 0;
vector<int> vec;
int res_flag = 1;
for (int i = 0; i < needle.size(); ++i)
{
if (flag)
{
for (int num = 0; num < vec.size(); ++num)
{
res_flag = 1;
for (int j = vec[num] + 1,temp=1; j < vec[num] + needle.size(); ++j, ++temp)
{
if (needle[temp] != haystack[j])
{
res_flag = 0;
break;
}
}
if (res_flag)
return vec[num];
}
return -1;
}
for (int j = 0; j < haystack.size(); ++j)
{
if (needle[i] == haystack[j])
{
vec.push_back(j);
flag = 1;
}
}
if (!flag)
return -1;
}
return 0;
}
方法二(目前最好):