28. Implement strStr()
2016-01-06 08:50
603 查看
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解题思路:
brute-force 复杂度为O(MN)。下次有机会写一个O(n)的KMP
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解题思路:
brute-force 复杂度为O(MN)。下次有机会写一个O(n)的KMP
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if len(needle)==0:return 0 for i,c in enumerate(haystack): tmp = haystack[i:i+len(needle)] if tmp==needle:return i return -1
class Solution { public: int strStr(string haystack, string needle) { int n = haystack.size(); int m = needle.size(); if(m==0)return 0; for(int i=0;i<=n-m;i++) { string tmp = haystack.substr(i,m); if(tmp==needle)return i; } return -1; } };
相关文章推荐
- 众筹是个“故事大王”
- 本地配置域名
- 2016年1月5日知识回顾
- 2015年年底由于项目需要,边学习边开发项目学习了游标和存储过程 特此记录
- 算法复杂度
- js判断当前页面在移动设备还是在PC端中打开
- [从头学数学] 第31节 表内除法(二)
- C#中Base64之编码,解码方法
- UI键盘通知
- iOS 延时执行的四种方法
- 反射获取泛型真实类型
- 机器视觉中的图像积分图及其实现
- 熔断“首秀”券商营业部侧记:“有点措手不及”
- IOS(UI)_UIButton(按钮)和UIImageView(图片视图)
- apktool升级到2.0以上时,使用方式已经替换
- [javase学习笔记]-7.13 静态的内存加载
- winform按钮美化(非图片)
- js判断当前页面用什么浏览器打开的方法
- javascript中闭包(Closure)详解
- velocity模板引擎 -- java.io.FileNotFoundException: velocity.log (Permission denied)