C++ Solution


  • 0
    F

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


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.