[LeetCode] 028. Implement strStr() (Easy) (C++/Python)
来源:互联网 发布:人工智能的未来 mobi 编辑:程序博客网 时间:2024/05/23 14:41
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode
028. Implement strStr() (Easy)
链接:
题目:https://oj.leetcode.com/problems/implement-strstr/
代码(github):https://github.com/illuz/leetcode
题意:
在一个字符串里找另一个字符串在其中的位置。
分析:
这题归在 Easy 类是因为它 O(n*n) 的暴力能过。
如果数据强点就得 Midium 以上了。
- (C++) 这题的暴力 O(n^2) 就是两遍 for 去找
- (C++) 还有各种高大上的算法,比如 KMP 算法,这是经典的了,具体实现见 我的另一篇博客。
- (Python) 另外可以用 hash 去做,叫 rolling hash 算法(见 Wiki 和 StackOverflow),就是把字符串 hash 出来,按匹配串长度窗口去滚动,再去匹配。hash 字符串有很多种方法,这边的字母好像都是小写,有 26 个,所以就用 29 做基数(本来想像 djb2 算法用 33 做基数,可以直接
((hash << 5) + hash)
很快,不过 int 范围只能 hash 6 个字母而且 rolling 的时候还是要/33
,还是用 29 算了),超 int 范围的话用 Python 就不用考虑这个问题了。
其他还有 Boyer–Moore,Rabin–Karp 算法,具体自己去搜。
代码:
C++: 暴力
class Solution {public: int strStr(char *haystack, char *needle) {int hlen = strlen(haystack);int nlen = strlen(needle);if (hlen < nlen)return -1;for (int i = 0; i <= hlen - nlen; i++) {if (!strncmp(haystack + i, needle, nlen))return i;}return -1; }};
C++: KMP
class Solution {private:int next[1000100];public: int strStr(char *haystack, char *needle) {int hlen = strlen(haystack);int nlen = strlen(needle);int i, j;if (hlen < nlen)return -1;if (nlen == 0)return 0;// KMP// get next matching array// int *next = new int(nlen + 1);// Because the string will be 1 million, so we cannot use `int *next = new int(nlen + 1)`, it will exceed the memory in functioni = 0;j = -1;next[0] = -1;while (i < nlen) {if (j == -1 || needle[i] == needle[j]) {i++;j++;next[i] = j;} elsej = next[j];}// matchingi = 0;j = 0;while (i < hlen) {if (j == -1 || haystack[i] == needle[j]) {i++;j++;} elsej = next[j];if (j == nlen)return i - nlen;}return -1; }};
Python:
class Solution: # @param haystack, a string # @param needle, a string # @return an integer def strStr(self, haystack, needle): hlen, nlen = len(haystack), len(needle) if nlen == 0: return 0 if nlen > hlen or hlen == 0: return -1 rolling = lambda x, y: x * 29 + y get_hash = lambda ch: ord(ch) - ord('a') nhash = reduce(rolling, map(get_hash, needle)) hhash = reduce(rolling, map(get_hash, haystack[:nlen])) if nhash == hhash: return 0 high_base = 29 ** (nlen - 1) for i in range(nlen, hlen): hhash -= get_hash(haystack[i - nlen]) * high_base # remove first in hash code hhash = rolling(hhash, get_hash(haystack[i])) # add new if nhash == hhash: return i - nlen + 1 return -1
1 0
- [LeetCode] 028. Implement strStr() (Easy) (C++/Python)
- Leetcode[easy] - Implement strStr()
- LeetCode-Easy-Java-Implement strStr()
- 28. Implement strStr() [easy] (Python)
- LeetCode刷题(C++)——Implement strStr()(Easy)
- Leetcode-Implement strStr()-Python
- 【Leetcode】【python】Implement strStr()
- LeetCode--Implement strStr() Python
- LeetCode 028. Implement strStr()
- 【LeetCode】028.Implement strStr()
- 【LeetCode】 028. Implement strStr()
- LeetCode-28-Implement strStr()(Sting/穷举)-Easy
- Leetcode 28. Implement strStr() (Easy) (cpp)
- LeetCode OJ 28 Implement strStr() [Easy]
- LeetCode-Easy刷题(9) Implement strStr()
- LeetCode 28 Implement strStr() (C,C++,Java,Python)
- [leetcode:python]28.Implement strStr()
- 【LeetCode 28】Implement strStr() (Python)
- cable master POJ 1064
- beego框架获取http请求内容
- 19、MySQL-查询表达式
- 查找网站搬迁错误Unable to connect to your database server using the provided settings
- Ubuntu 配置 Android 开发环境遇到的问题
- [LeetCode] 028. Implement strStr() (Easy) (C++/Python)
- FlappyBird开发总结(六)—— 计分制的实现
- Scala 点滴:参数序列 : _ *
- Qt开发 槽函数定义,以及槽函数二次响应多次响应问题
- 20、MySQL-带条件查询(WHERE)
- Openstack development trend
- 程序员必须知道的10大基础实用算法及其讲解
- win8-txt文件不能默认记事本打开
- iOS-获取苹果设备信息