1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()) return 0; int n = haystack.size(); int m = needle.size(); for(int i = 0; i < n; ++i) { if(haystack[i] == needle[0]) { int k = 1; for(; k < m && i + k < n && haystack[i + k] == needle[k]; ++k) {} if(k == m) return i; } } return -1; } };
|