[Leetcode] implement strStr() (C++)
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算法
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);
}
};
[Leetcode] implement strStr() (C++)的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
随机推荐
- VC实用小知识总结 (一),转http://blog.csdn.net/myiszjf/article/details/10007431
在上一篇中,我们以经介绍了程序的流程和框架,在本篇将详细讨论各个功能的实现主要包括 1.获取磁盘信息2.获取目录信息3.获取文件信息4.运行指定文件5.删除指定文件6.删除指定目录7.创建指定目录8. ...
- 乱七八糟Nodejs系列二:线程模型
上一篇中说了这样一句话:Nodejs和浏览器javascript一样,都是单线程,所以和传统的不一样,这个后面有机会再说.挖了坑就得填,哎. 1.一个例子 来看一个例子,这个例子来自async jav ...
- IE6~9的css hack写法
_color: red; /* ie6 */ *color: red; /* ie6/7 */ +color: red; /* ie6/7 */ color: red\0; /* ie8/9 */ c ...
- twemproxy 安装
twemproxy 安装 1. 获取安装包 shell> wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz shell> ...
- ECSTORE日期时间控件转换为时间戳的方法
如果你在后台使用<{input type=”time” name=”time”}>的话,后台获取到的表单数据为: ["_DTYPE_TIME"]=> array( ...
- WDCP控制面板的常用liunx命令集
WDCP是在linux下的一款常用的服务器可视化管理面板,是新手使用linux搭建网站的福音.本文不过多的介绍WDCP是什么,如果需要了解的话,可以至WDCP官方介绍页面查看. 今天博主准备查看网站系 ...
- Java学习笔记--xml构造与解析之Sax的使用
汇总:xml的构造与解析 http://www.cnblogs.com/gnivor/p/4624058.html 参考资料:http://www.iteye.com/topic/763895 利用S ...
- 蚁群算法matlab实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下用matlab实现蚁群算法: %蚂蚁算法test %用产生的一个圆上的十个点来检验蚂蚁 ...
- Objective-C 静态变量 使用方法
详解Objective-C中静态变量使用方法 Objective-C中静态变量使用方法是本文要介绍的内容,Objective-C 支持全局变量,主要有两种实现方式:第一种和C/C++中的一样,使用&q ...
- qt实现头像上传功能(写了4个类,朝十晚八的博客,非常好)
想必大家都使用过qt的自定义头像功能吧,那么图1应该不会陌生,本片文章我就是要模拟一个这样的功能,虽然没有这么强大的效果,但是能够满足一定的需求. 图1 qq上传图片 首先在讲解功能之前,我先给出一片 ...