Implement strStr()
2015-12-07 19:59
316 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Subscribe to see which companies asked this question
Show Tags
Hide Similar Problems
(H) Shortest Palindrome
1.普通匹配 o(n*m)
2.KMP o(n)
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Subscribe to see which companies asked this question
Show Tags
Hide Similar Problems
(H) Shortest Palindrome
1.普通匹配 o(n*m)
class Solution { public: int strStr(string haystack, string needle) { int haystackSize = haystack.size(); int needleSize = needle.size(); if(needleSize ==0){ return 0; } int resIndex = 0; int i=0,j=0,k=0; while(i<haystackSize && j<needleSize){ if(k>=haystackSize){ break; } if(haystack[k]==needle[j]){ k++; j++; }else{ i++; k = i; j = 0; } } resIndex = j==needleSize ? i:-1; return resIndex ; } };
2.KMP o(n)
相关文章推荐
- transition学习笔记~~
- 前端面试题准备 3
- Light OJ 1043 三角形比例问题
- 目前自己的几种脱壳方式
- Java开发 Eclipse使用技巧
- 理解update、upgrade和dist-upgrade的区别
- javascript中window.documen
- homerHEVC代码阅读(17)——基础结构之cu_partition_info_t
- Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治
- C#操作XML的方法
- ACM学习历程—UVALive 7147 World Cup(分类讨论 && 贪心)
- Lawnmower
- 监控MySQL主从同步延时
- 做spark的一点小感悟
- 发现C++Builder 2010一组类BUG
- POJ 1488 TEX Quotes
- hdu1180诡异的楼梯【优先队列+广搜】
- css 里面 ie8 支持css3的rgba()
- 2015第50周一了解微服务架构
- 【iOS】Assertion failure in -[MASViewConstraint install]