[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 ...
随机推荐
- Ajax调用返回json,xml数据类型(0517--pm)
一.返回Json型数据: 1.主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- CREATE DATABASE
CREATE DATABASE IF NOT EXISTS `focusdata` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `fo ...
- jq改变DIV中图片的路径
<script src="common/jquery.js"></script> <script language="javascript ...
- C语言初学 if-else语句判断俩数的最大值
#include<stdio.h> main() { float a,b; printf("输入俩个任意实数\n"); scanf("%f%f",& ...
- 通过布局文件来显示ListView内容并注册 ListView事件
1:layout/vlist.xml是我们的布局文件,在这里一定要对首节点加上 android:descendantFocusability="blocksDescendants" ...
- 黑马程序员_Java_String
String类 一.概述 字符串是一个特殊的对象. 字符串一旦初始化就不可以被改变. String s1 = "abc";//s1是一个类类型变量,"abc"是 ...
- [iOS] Create TableView & customize UITableViewCell
1. First artical, notice the last thing - Connecting the DataSource and Delegate: http://www.appcoda ...
- JAVA泛型编程笔记
1介绍 Java泛型编程是JDK1.5版本后引入的.泛型让编程人员能够使用类型抽象,通常用于集合里面. 下面是一个不用泛型例子: List myIntList=new LinkedList(); // ...
- python glob标准库基础学习
#glob文件名模式匹配#作用:使用unix shell规则查找与一个模式匹配文件名"""尽管glob api很小,但这个模块很强大,只要程序需要查找文件系统中名字与某种 ...
- Vue + element-ui
在Vue-cli生成的项目中使用 element-ui,按照官方的指导 npm i element-ui -D 执行之后,查看package.json,element-ui 加在了 "dev ...