第14天

遍历,如果发现与第一位相同的元素,向后检查是否一致,不一致弹出,如果遍历完成仍未发现,返回-1.

int strStr(string haystack, string needle) {
    
    
    int s2 = needle.size(),s1 = haystack.size() - s2;
    if(s2 == 0) return 0;
    if(s1 < 0) return -1;
    bool test;
    for(int r = 0;r <= s1;++r){
    
    
        if(haystack[r] == needle.front()) test = true;
        else test = false;
        for(int n = 0;n < s2 && test;++n){
    
    
            if(haystack[r + n] != needle[n])
                test = false;
        }
        if(test)
            return r;
    }
    return -1;
}

猜你喜欢

转载自blog.csdn.net/AgaSS1225/article/details/113117029
今日推荐