LintCode 简单-13. 字符串查找

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 18:54   1183   0
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实现一个并得到一些启示(一次思维的转弯
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP