lintcode 28. Implement strStr()

image.png
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) {
        // write your code here
        //if(!source && !target) return 0;
        
        if(!source || !target) return -1;
        if(strlen(target) == 0 ) return 0;
        for(int i = 0; i < strlen(source); i++){
            if(source[i] != target[0]){
                continue;
            }
            int j = 1;
            for(;j < strlen(target); j++){
                if(source[i+j] != target[j]){
                    break;
                }
            }
            if(j == strlen(target)){
                return i;
            }
        }
        return -1;
    }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容