码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode ImplementStrstr

时间:2014-06-11 07:40:05      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

bubuko.com,布布扣
class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        if (haystack == NULL || needle == NULL) return NULL;
        int wpos[256];
        char cur = 0;
        int len = 0;
        for (int i=0; i < 256; i++) wpos[i] = -1;

        for (int i=0; (cur =  needle[i]) != \0; i++, len++) wpos[cur] = i;
        int p = 0, q = 0;
        while (true) {
            while (haystack[p] != \0
                    && needle[q] != \0
                    && haystack[p] == needle[q]) p++, q++;

            if ( needle[q] == \0) {
                return haystack + p - q;
            }

            if (haystack[p] == \0) {
                return NULL;
            }

            int npos = p - q + len;
            int move = wpos[haystack[npos]];
            q = 0;
            if (move < 0) {
                p++;
            } else {
                p = npos - move;
            }
        }
        return NULL;
    }
};
bubuko.com,布布扣

又写了次sunday算法,还是磕磕碰碰

LeetCode ImplementStrstr,布布扣,bubuko.com

LeetCode ImplementStrstr

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/lailailai/p/3773561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!