Implement strStr()

来源:互联网 发布:linux vi 怎么查找 编辑:程序博客网 时间:2024/06/09 21:58

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) {        if(needle == ""){            return 0;        }                if(haystack.length()<needle.length()){            return -1;        }                for(int i=0;i<=haystack.length()-needle.length();i++){            int j;            for(j=0;j<needle.length();j++){                if(haystack[i+j] != needle[j])                    break;            }            if(j == needle.length()){                return i;                break;            }        }        return -1;            }};


0 0
原创粉丝点击