Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode

题目:

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 a char * or String, please click the reload button  to reset your code definition.

Tag:

String; Two Pointers

体会:

这道题是字符串的匹配问题,目前还只会朴素的逐步方法,但是这道题还有KMP解法,和Boyer-Moore的解法,目前还没有掌握。

先把笔记放到这里,以后再来解决吧。

1. KMP算法

2. Boyer-Moore算法

 class Solution {
public:
int strStr(char *haystack, char *needle) {
int i = ; // index under examine in haystack
int j = ; // index under examine in needle while (haystack[i] && needle[j]) {
if (haystack[i] == needle[j]) {
i++;
j++;
} else {
// roll back:
// reset haystack index back to (i - j + 1), since in current examine
// start index in haystack is (i - j)
i = i - j + ;
// reset needle back to its beginning
j = ;
}
}
// if haystack ends but needle not ends,
// we know needle is not in the haystack, then return -1.
return (needle[j]) ? - : (i - j);
}
};

最新文章

  1. C# 6.0 功能预览 (一)
  2. hihoCoder 后缀自动机三·重复旋律6
  3. JDK自带工具列表
  4. 安装php-amqplib(RabbitMQ的phpAPI)
  5. HBase 实战(2)--时间序列检索和面检索的应用场景实战
  6. Uncaught ReferenceError: WebForm_DoPostBackWithOptions is not defined
  7. JavaScript基础18——js的Array对象
  8. XproerIM V1,2,12,65376 发布。
  9. 技术英文单词贴--G
  10. Java开发者值得关注的7款新工具
  11. 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法
  12. 安装Oracle11g时,检测到系统的主 IP 地址是 DHCP 分配的地址
  13. webservice发布接口
  14. JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别
  15. [Codeforces Round #192 (Div. 2)] D. Biridian Forest
  16. 简单理清一下proto与prototype
  17. Delphi引用C对象文件
  18. TabBar自定义方式(一)
  19. 用图来教你怎样用Photoshop蓝底转换红底
  20. QLayout窗口布局

热门文章

  1. 3.class文件基本结构
  2. include,include_once,require,require_once的区别
  3. Request对象 --web浏览器向web服务端的请求
  4. DPI情况下处理
  5. Ruby中的Symbol与字符串
  6. Android android.support.v7.appcompat.R$styleable
  7. 自定义DTD(myeclipser的XML提示功能)
  8. Oracle中对列加密的方法
  9. bzoj1151
  10. TCP/IP 中文译名为传输控制协议/因特网互联协议,又叫网络通讯协议