1. 程式人生 > >LeetCode 28. Implement strStr() 實現strStr()

LeetCode 28. Implement strStr() 實現strStr()

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle=="")
            return 0;
        int j=0,n=-1;
        bool ISstr;
        for(int i=0;i<haystack.size();i++)
        {
            if(haystack[i]!=needle[0])
                continue;
            else
            {
                n=i;
                for(int k=0;k<needle.size();k++)
                {
                    if(haystack[i]==needle[k])
                    {
                        i++;
                        ISstr=true;
                    }
                    else
                    {
                        i=n;
                        n=-1;
                        ISstr=false;
                        break;
                    }
                }
            }
            if(ISstr==true)
                break;
        }
        return n;
    }
};