[LeetCode] Implement strStr()
代码:
暴力算法
1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 if (!*needle) 5 return 0; 6 int alen = strlen(haystack); 7 int blen = strlen(needle); 8 int i = 0, j = 0; 9 while (i < alen && j < blen) { 10 if (haystack[i] == needle[j]) { 11 i++; 12 j++; 13 } else { 14 i = i - j + 1; 15 j = 0; 16 } 17 } 18 if (j == blen) { 19 return i - j; 20 } else { 21 return -1; 22 } 23 } 24 };
KMP算法
1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 return kmp(haystack, needle); 5 } 6 private: 7 int kmp(const char *text, const char *pattern) { 8 int i = 0; 9 int j = 0; 10 const int n = strlen(text); 11 const int m = strlen(pattern); 12 int *next = (int*)malloc(sizeof(int)* m); 13 compute_prefix(pattern, next); 14 15 while (i < n && j < m) { 16 if (j == -1 || text[i] == pattern[j]) { 17 i++; 18 j++; 19 } else { 20 j = next[j]; 21 } 22 } 23 24 free(next); 25 if (j == m) 26 return i - j; 27 else 28 return -1; 29 } 30 31 void compute_prefix(const char *pattern, int next[]) { 32 int i = 0; 33 int j = -1; 34 const int m = strlen(pattern); 35 next[0] = -1; 36 37 while (i < m - 1) { 38 if (j == -1 || pattern[i] == pattern[j]) { 39 i++; 40 j++; 41 if (pattern[i] != pattern[j]) 42 next[i] = j; 43 else 44 next[i] = next[j]; 45 } 46 else { 47 j = next[j]; 48 } 49 } 50 } 51 };
杂记:
参考:http://blog.csdn.net/v_july_v/article/details/7041827
转载于:https://www.cnblogs.com/Azurewing/p/4311819.html
[LeetCode] Implement strStr()相关推荐
- Leetcode: Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needl ...
- Leetcode | Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- LeetCode Implement strStr(kmp或者BM)
题意:求一字符串在另一字符串的中的索引 思路:kmp 代码如下: public class Solution {private int[] kmp_table(String s){int[] next ...
- LeetCode集锦(十) - 第28题 Implement StrStr
LeetCode集锦(十) - 第28题 Implement StrStr 问题 Implement strStr().Return the index of the first occurrence ...
- LeetCode - Easy - 28. Implement strStr()
Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...
- LeetCode算法入门- Implement strStr() -day22
LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...
- leetcode python3 简单题28. Implement strStr()
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...
- 【LeetCode】28. Implement strStr()
Difficulty:easy More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
最新文章
- javascript里的偏函数——本质函数式编程+闭包,返回函数
- Win10(UEFI启动)安装Ubuntu18.04双系统
- Matlab 常用语法速记 1
- 在网页在播放flv格式的视频
- Linux设备驱动之字符设备(二)
- GetTickCount64的使用
- 统计学 | 八大经典思想「AI核心算法」
- 开新林哲自曝:打造二手车全球经营
- 全球定位经纬度的方法现在是GPS等系统定位,以前是指南针罗盘六分仪
- 项目中引入阿里巴巴矢量字体图标库
- HDU - 1548 A strange lift
- x86服务器MCE(Machine Check Exception)问题
- 学习笔记之——路径规划
- singleTask vs singleInstance
- 卷积神经网络(CNN):乳腺癌识别
- Android 忆童年 DVD 待机动画
- QQ出现大规模盗号,为什么会这样?就没有解决方法了吗?
- WeWork入华 盈利奇迹能否复制
- 来去之间:微博第四季度净营收4.819亿美元 同比增长28%
- SuperSocket