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