2019-02-03 第七天(#28)

#28 Implement strStr()

题目地址:https://leetcode.com/problems/implement-strstr/
这道题easy估计是easy在了暴力搜索实现上。要达到O(n)的最优解需要KMP算法,而KMP算法我觉得难得不行。

初见(时间复杂度O(mn))

简单暴力的brute force:

class Solution {
public:
    
    int strStr(string haystack, string needle) {
        if(needle.size() == 0) return 0;
        else if(needle.size() > haystack.size()) return -1;
        
        for(int indh = 0; indh < haystack.size() - needle.size() + 1; indh++){
            for(int indn = 0; indn < needle.size(); indn++){
                if(haystack.at(indh + indn) != needle.at(indn))
                    break;
                else if(indn == needle.size() - 1)
                    return indh;
            }
        }
        
        return -1;
    }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 来自这个兄弟:http://blog.csdn.net/ddd_1206/article/category/685...
    580aa87075d3阅读 4,811评论 0 18
  • 原题链接: http://oj.leetcode.com/problems/implement-strstr/ R...
    DrunkPian0阅读 1,131评论 0 0
  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 18,066评论 2 36
  • 今天,同事的一句话点醒了我。一个同事在不停地抱怨着生活,另一个同事一句“你就像个怨妇!”噎住了她的抱怨。我不...
    秋之思阅读 2,812评论 0 1
  • 关于如何避免每天加班的一个思考。 1.客观任务重 一般在开发中后期,适当加班。制定计划,量化工作,各个击破。 2 ...
    逆旅行人ing阅读 1,196评论 0 1