LeetCode-Implement strStr()-字符串查找

https://oj.leetcode.com/problems/implement-strstr/

简单的调用string::find就可以解决。高级的可以自己实现KMP或者移动哈希算法。

string::find返回一个size_t,如果没找到其返回值为-1(string::npos)。

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        string s1(haystack);
        string s2(needle);
        size_t p=s1.find(needle);
        if (p!=string::npos){
            return haystack+p;
        }
        return NULL;
    }
};

  

posted @ 2014-10-08 19:41  zombies  阅读(146)  评论(0编辑  收藏  举报