Leetcode Implement strStr()

来源:互联网 发布:苏州程序员平均工资 编辑:程序博客网 时间:2024/06/05 00:49

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 i;        int result = 0;        bool flag = false;        int len = needle.length();        if(len==0&&haystack.length()==0)            return 0;        for(i=0; i<haystack.length();i++)        {            if(strcmp(haystack.substr(i,len).c_str(),needle.c_str())==0)            {                flag = true;                break;            }        }        if(flag)        {            return i;        }        else            return -1;    }};


0 0
原创粉丝点击