zoukankan      html  css  js  c++  java
  • [leetcode]Implement strStr()

    一开始写了个KMP算法,大计算量居然超时,后来用最简单的蛮力算法,居然过了。。

    class Solution {
    public:
        char *strStr(char *haystack, char *needle) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
             
            int hayLen = strlen(haystack);
            int neeLen = strlen(needle);
    
            for (int i = 0; i <= hayLen - neeLen; i++){
                char *p = haystack + i;
                char *q = needle;
    
                while(*q != '\0'){
                    if (*p == *q){
                        p++;
                        q++;
                    }
                    else
                        break;
                }
    
                if (*q == '\0')
                    return p - neeLen;
            }
            return NULL;
        }
     };

    EOF

  • 相关阅读:
    用python将博客园的文章爬取到本地
    2016.7.9
    2016.7.8
    2016.7.7
    2016.7.5
    2016.7.4
    2016.7.3
    2016.7.2
    2016.6.28
    2016.6.27
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2853819.html
Copyright © 2011-2022 走看看