class Solution {
public:
/*
* @param source: source string to be scanned.
* @param target: target string containing the sequence of characters to match
* @return: a index to the first occurrence of target in source, or -1 if target is not part of source.
*/
int strStr(const char *source, const char *target) {
if(source == NULL || target == NULL)
{
return -1;
}
else
{
int i = 0,tar = 0;
if(target[tar] == '\0')
return 0;
for(;source[i] != '\0';i++)
{
if(source[i] == target[tar])
{
int temp = i;
for(;target[tar] != '\0';tar++,temp++)
{
if(source[temp] != target[tar])
break;
}
if(target[tar] == '\0')
return i;
tar = 0;
}
}
return -1;
}
}
};
认知:①理解了KMP字符串查询优化算法②用JAVA实现一个并得到一些启示(一次思维的转弯)
|