LeeCode27:Implement strStr()
来源:互联网 发布:改键位软件 编辑:程序博客网 时间:2024/05/25 19:58
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
public class Solution { public String strStr(String haystack, String needle) { // Start typing your Java solution below // DO NOT write main() function String res = null; if(needle.length()==0) return haystack; if(needle.length()!=0 && haystack.length()==0) return res; // build Table int cnd=0, pos=2; int [] next = new int[needle.length()]; next[0]=-1; if(needle.length()>1) next[1]=0; while(pos<needle.length()){ // forward if(needle.charAt(cnd)==needle.charAt(pos-1)){ cnd++; next[pos]=cnd; pos++; } // fall back else if(cnd>0){ cnd = next[cnd]; continue; } // unluck no macth else{ next[pos]=0; pos++; } } int hPos=0, nPos=0; while(hPos+nPos<haystack.length()){ if(haystack.charAt(hPos+nPos)==needle.charAt(nPos)){ nPos++; if(nPos == needle.length()){ res = haystack.substring(hPos); break; } } else{ hPos = hPos+nPos-next[nPos]; if(next[nPos]!=-1) nPos = next[nPos]; else nPos = 0; } } return res; }}
- LeeCode27:Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- 三维显示软件制作--显示部分(4)
- 洗牌算法多种形式
- ubuntu 10.04下成功编译Qtopia2.2.0
- (转载) UITableView 系列一 :基本使用方法 (显示,删除,添加图片,添加样式等
- C# 小程序之新手练习(三)数组模拟约瑟夫环
- LeeCode27:Implement strStr()
- 分享一位软件工程师的7年总结
- 异步消息的传递-回调机制
- Objective C的学习之HelloWorld
- 【学习笔记】python版选择排序和插入排序及时间复杂度分析
- POJ2151--Check the difficulty of problems
- Android JNI入门实例(Windows+Cygwin+Eclipse)
- property()
- [LeetCode] 一个BST的两个节点的值被交换,要求修正这个BST