24 - Implement strStr()

来源:互联网 发布:两年经验的程序员工资 编辑:程序博客网 时间:2024/06/16 14:07

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.


solution: 方法一,最直观和最没效率的自然是一遍又一遍的遍历比对;

方法二,在方法一的实现过程中,我们能遇到这些情况,遍历的过的字符串会一再的遍历,然而很多情况是可以剪除,直接跳到合适的位置上的,所以,这里我们想到了两个引申的算法,kmp和boyer-moore算法,这里我仅实现了boyer-moore算法,该算法的原理就是在遍历时同时记录计算下一个跳转的位置,根据的原理是下一个字符在比对字符串中出现的位置,参看:

wiki: http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm

一个blog:http://www.ruanyifeng.com/blog/2013/05/boyer-moore_string_search_algorithm.html,这篇文章里详细介绍了该算法,然后引用了算法创始人的一个demo:http://www.cs.utexas.edu/~moore/best-ideas/string-searching/fstrpos-example.html,跟着跑一遍很有帮助。

方法三,看了leetcode介绍的该题tutor:http://leetcode.com/2010/10/implement-strstr-to-find-substring-in.html, 还有其它算法,当然它这里介绍的可能还是不够多,不过对于我来说只了解了里面三个算法就差不多了。


这里实现了Boyer-moore算法,但是没加判断后缀,以后再做

class Solution {public:    int locateCh(char *needle, char target, int length)    {        int offset = length - 1;        while( needle[offset] != target )        {            if( offset <= 0 )                return -1;            offset --;        }        return offset;    }    char *strStr(char *haystack, char *needle) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int len = 0;        int index = 0;                if(needle == NULL || haystack == NULL)            return NULL;        else if(*needle == '\0')            return haystack;                while(needle[len] != '\0')        {            if(haystack[ index ] == '\0')                return NULL;            len++;            index++;        }                int longlen = 0;        while( haystack[longlen] != '\0' )        {            longlen++;        }                index -= 1;        len -= 1;                while( index < longlen )        {            int bad = len;            int in = index;            while( haystack[in] == needle[bad] )            {                if(bad <= 0)                    return haystack+in;                bad --;                in --;            }                        int offset = locateCh(needle, haystack[in], bad);            int move = bad - offset;                        index += move;        }        return NULL;    }};




原创粉丝点击