using c++ solve this problems


  • 0
    B

    、、、
    class Solution {
    public:
    int strStr(string haystack, string needle) {

        int LenNeedle = needle.length();
        int LenHaystack = haystack.length();
        string Temp;
        if((!LenNeedle && !LenHaystack) || (!LenNeedle && LenHaystack > 0)) return 0;
        if(!LenNeedle || !LenHaystack || LenNeedle > LenHaystack) return -1;
        
        if(needle == haystack) return 0;
        
        for(int i = 0; i < LenHaystack - LenNeedle + 1; i++)
        {
            Temp = haystack.substr(i,LenNeedle);
            if(Temp == needle)
            {
                return i;
            }
        }
        return -1;        
    }
    

    };
    、、、、


Log in to reply
 

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