leetcode-----28. 实现 strStr()

代码

class Solution {
public:
    int strStr(string haystack, string needle) {
        int n = haystack.size();
        int m = needle.size();

        for (int i = 0; i < n - m + 1; ++i) {
            bool flag = true;
            for (int j = 0; j < m; ++j) {
                if (haystack[i + j] != needle[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return i;
            }
        }
        return -1;
    }
};
posted @ 2020-06-20 15:26  景云ⁿ  阅读(53)  评论(0编辑  收藏  举报