【LeetCode028】Implement strStr()

class Solution {
public:
    int strStr(string haystack, string needle) {
        int len_h = haystack.length();
        int len_n = needle.length();
        if(len_n == 0)
            return 0;
        if(len_h < len_n)
            return -1;
        for(int i = 0; i <= len_h-len_n; ++i){
            if(!(haystack.compare(i, len_n, needle)))
                return i;
        }
        return -1;
    }
};

需要比较两个字符串的长度

猜你喜欢

转载自blog.csdn.net/weixin_39458342/article/details/87182523