代码:

暴力算法

 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()相关推荐

  1. Leetcode: Implement strStr()

    Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needl ...

  2. Leetcode | Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  3. LeetCode Implement strStr(kmp或者BM)

    题意:求一字符串在另一字符串的中的索引 思路:kmp 代码如下: public class Solution {private int[] kmp_table(String s){int[] next ...

  4. LeetCode集锦(十) - 第28题 Implement StrStr

    LeetCode集锦(十) - 第28题 Implement StrStr 问题 Implement strStr().Return the index of the first occurrence ...

  5. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  6. LeetCode算法入门- Implement strStr() -day22

    LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...

  7. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  8. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

  9. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

最新文章

  1. javascript里的偏函数——本质函数式编程+闭包,返回函数
  2. Win10(UEFI启动)安装Ubuntu18.04双系统
  3. Matlab 常用语法速记 1
  4. 在网页在播放flv格式的视频
  5. Linux设备驱动之字符设备(二)
  6. GetTickCount64的使用
  7. 统计学 | 八大经典思想「AI核心算法」
  8. 开新林哲自曝:打造二手车全球经营
  9. 全球定位经纬度的方法现在是GPS等系统定位,以前是指南针罗盘六分仪
  10. 项目中引入阿里巴巴矢量字体图标库
  11. HDU - 1548 A strange lift
  12. x86服务器MCE(Machine Check Exception)问题
  13. 学习笔记之——路径规划
  14. singleTask vs singleInstance
  15. 卷积神经网络(CNN):乳腺癌识别
  16. Android 忆童年 DVD 待机动画
  17. QQ出现大规模盗号,为什么会这样?就没有解决方法了吗?
  18. WeWork入华 盈利奇迹能否复制
  19. 来去之间:微博第四季度净营收4.819亿美元 同比增长28%
  20. SuperSocket

热门文章

  1. 会计的思考(20):还原会计报表的企业个性之四(持续经营)
  2. 用AjaxPro实现无刷新翻页效果及数据库分页技术介绍
  3. [NOI2017]游戏(2-SAT)
  4. MariaDB 主从复制的配置
  5. python操作webservices
  6. ARM7+PROTEUS调试(转)
  7. php代码 2012057
  8. 如果说一个地图是1000*1000那么需要多少内存呢?
  9. 【剑指offer-Java版】40数组中只出现一次的数字
  10. Activity 启动模式以及常见的启动Flag