My c++ solution using find


  • -3
    C
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            result = (size_t)haystack.find(needle);
            if(result <= haystack.size())
                return result;
            else
                return -1;
        }
    private:
        int result;
    };
    

    I come up with this solution when I first saw the question. It passes all the test cases. But not sure if it is okay for this one though.


  • -2
    M

    The functionalities of strStr and string.find are equal, It is meaningless to implement each other.
    Besides your variable result is not necessary to be a member of class, just use a temporary.


Log in to reply
 

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