LeetCode 28 StrStr 解法为KMP&&Rolling hash
来源:互联网 发布:数据工具培训心得 编辑:程序博客网 时间:2024/05/21 08:55
Implement strStr()
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.
代码如下:
public int strStr(String haystack, String needle) { int al = haystack.length(); int bl = needle.length(); if(bl==0){ return 0; } if(al<bl){ return -1; } int i=0; while(i < al) { int j=0; int t = i;while(j!=bl){if(needle.charAt(j)==haystack.charAt(t)){j++;t++;if(t>=al){break;}}else{break;}}if(j==bl){return i;}i++;} return -1; }
当然除了KMP之外也有其他的线性算法,比如Rolling hash。
见博客:
http://blog.csdn.net/linhuanmars/article/details/20276833
0 0
- LeetCode 28 StrStr 解法为KMP&&Rolling hash
- Leetcode 28 Implement strStr() KMP算法
- leetcode 28: Implement strStr() (KMP算法)
- LeetCode-28-Implement strStr() KMP模板题
- LeetCode Implement strStr() --- KMP
- Implement strStr() [LeetCode] + KMP
- kmp-----leetcode(Implement strStr() )
- rolling hash
- [LeetCode] Implement strStr()[字符串hash]
- [LeetCode-28] Implement strStr(KMP 字符串匹配)
- leetcode 28. Implement strStr() 三种解法
- strStr算法(无KMP) 略微优化的暴力解法
- [Leetcode] Implement strStr()与 KMP算法
- 【leetcode KMP算法实现】Implement strStr()
- leetcode系列(43)strStr(KMP)
- Leetcode 28. Implement strStr() KMP算法解题
- LeetCode Implement strStr(kmp或者BM)
- leetcode 28. Implement strStr() KMP 算法
- android 添加视频、图片、录音上传(三)
- java后台服务器向手机端返回Json, 使用 "net.sf.json.JSONObject" 解析出现的异常问题
- R语言_基本统计分析
- 【Windows编程】系列第十篇:文本插入符
- 类和接口的设计原则
- LeetCode 28 StrStr 解法为KMP&&Rolling hash
- win8下 node.js模块msnodesql的配置(附连接sqlserver2008 demo)
- 多线程 - 01.多线程概述
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- eclipse自动补全的设置
- 多线程 - 02.利用pthread实现多线程
- Allegro过孔打在焊盘上(via与pad重叠),如何显示DRC错误?
- Android-自定义发送/接收广播(BroadcastReceiver)
- extern 修饰全局变量深度全方位剖析