LeetCode:Implement strStr()

Implement strStr()

Total Accepted: 97192  Total Submissions: 396959  Difficulty: Easy

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Subscribe to see which companies asked this question

Hide Tags
  Two Pointers String
Hide Similar Problems
  (H) Shortest Palindrome


















code:

class Solution {
public:
    int strStr(string haystack, string needle) {
        int m = haystack.size();
        int n = needle.size();
        for(int i=0;i<m-n+1;i++){
            int j=0;
            for(;j<n;j++){
                if(haystack[i+j]!=needle[j]) break;
            }
            if(j==n) return i;
        }
        return -1;
    }
};



你可能感兴趣的:(LeetCode,strstr,implement)