LeetCode : Implement strStr()

来源:互联网 发布:指绘软件sketchbook 编辑:程序博客网 时间:2024/06/06 17:28

Implement strStr().

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

在haystack中查找needle第一次出现的位置:

class Solution{public:    int strStr(string haystack, string needle) {        if (needle.length() == 0)            return 0;        if (haystack.length() < needle.length())            return -1;        for (int i = 0; i<haystack.length(); ++i){            int j;            for (j = 0; j<needle.length(); ++j){                if (haystack[i + j] != needle[j])                    break;            }            if (j == needle.length())                return i;        }        return -1;    }};
0 0
原创粉丝点击