Implement strStr()

来源:互联网 发布:胸肌下轮廓 知乎 编辑:程序博客网 时间:2024/06/05 22:31

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.


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


原创粉丝点击