Implement strStr()
来源:互联网 发布:内容中心网络体系架构 编辑:程序博客网 时间:2024/04/29 09:40
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns achar *
or String
, please click the reload button to reset your code definition.
Solution:
class Solution {public: void getNext(string needle, vector<int> &next) { int i = 0, j = -1; next[i] = j; while(i < needle.length()) { while(j != -1 && needle[i] != needle[j]) j = next[j]; next[++i] = ++j; } } int strStr(string haystack, string needle) { vector<int> next(needle.length()+1); getNext(needle, next); int i = 0, j = 0; int hlen = haystack.length(); int nlen = needle.length(); while(i < hlen && j < nlen) { if(j == -1 || haystack[i] == needle[j]) { ++i; ++j; } else { j = next[j]; } } if(j == nlen) return i - j; else return -1; }};
0 0
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Comparing replay, replayLast, and replayLazily
- centos 安装phpmyadmin
- C++默认构造函数
- 校验和算法
- 面试经验
- Implement strStr()
- Laravel学习
- unix命令
- servlet知识点
- Qt中QStack简单应用
- 14_Android中Service的使用,关于广播接收者的说明
- Android.mk 详解剖析
- CSS3 小技巧/注意事项
- java九大内置对象