Leetcode-实现 strStr() 函数

9.实现 strStr() 函数

题目内容:

代码及思路:

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

猜你喜欢

转载自blog.csdn.net/larklili/article/details/89190688