28. Implement strStr()

Implement strStr().

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

实现strStr()函数:返回模式P在文本T中的索引,如果没有发现则返回-1。


Brute Force:(暴力求解法)

int strStr(char* haystack, char* needle) {
    int h_index=0;
    int n_index=0;
    if(*haystack=='\0'&&*needle=='\0') 
       h_index=0;
       else if(*haystack=='\0'&&*needle!='\0')
       h_index=-1;
    while(*haystack!='\0')           //判断haystack字符数组是否结束
    {
        while(*needle!='\0')         //判断needle字符数组是否结束
        {
            if(*haystack!=*needle)
            {
                haystack-=n_index;   //h指针回位
                needle-=n_index;     //n指针回位
                n_index=0;
                break;               //有一个字符不相等
            }
            else
            {
                haystack++;
                needle++;
                n_index++;
            }
        }
        if(*needle=='\0')           //确定是子字符串
            break;
        else                        //比较下一个
        {
            haystack++;
            h_index++; 
            if(*haystack=='\0')
            {
                h_index=-1;
                break;
            }
        }
    }
    return h_index;
}
思路很简单,匹配失败则移动一个字符继续匹配,最坏复杂度为O(n^2),在LeetCode OJ上提交时由于Time limit exceeded失败。


Sunday 算法

int strStr(char* haystack, char* needle) {
    int h_index,n_index,m,n=0;
    n=strlen(haystack);
    m=strlen(needle);
    if(*haystack=='\0'&&*needle=='\0')
    h_index=0;
    else if(*haystack=='\0'&&*needle!='\0'||n<m)
    h_index=-1;
    else
    {
        for(h_index=0;h_index<(n-m+1);)
        {
            for(n_index=0;n_index<m;n_index++)        //P与T逐一对比
            {
                if(*(haystack+h_index+n_index)!=*(needle+n_index))
                break;
            }
            if(n_index<m&&h_index==(n-m))            //最后一次匹配失败
            {
                h_index=-1;
                break;
            }
            if(n_index<m)                          //非最后一次匹配失败
            {
                h_index+=m;                            //T的下标移动至haystack+m处
                for(n_index=m-1;n_index>-1;n_index--)  //从右往左将P与haystack+m处字符比较
                {
                    if(*(haystack+h_index)==*(needle+n_index))
                    break;
                }
                if(n_index==-1&&(n-h_index+1)>m)             //匹配不上且容得下P,从haystack+m+1处开始比较
                h_index++;
                else if(n_index>-1&&(n-h_index)>=(m-n_index)) //匹配上且T能容下移动过的P,将P移至匹配字符处
                h_index-=n_index;
                else                                         //否则结束
                {
                    h_index=-1;
                    break;
                }
            }
            else                                        //直接匹配成功
            break;
        }
    }
    return h_index;
}
其核心思想是:在匹配过程中,模式串发现不匹配时,算法能跳过尽可能多的字符以进行下一步的匹配,从而提高了匹配效率。

提交结果如下


还有其他诸如KMP算法,但实现比较复杂我没有搞懂。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值