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.

算法:KMP算法,字符串比对

java:

public class Solution {
public int strStr(String haystack, String needle) {
int l1=haystack.length();
int l2 = needle.length();
if(l1<l2){
return -1;
}
if(l1==l2){
if(haystack.equals(needle)){
return 0;
}
return -1;
}
if(l2==0){
return 0;
}
int i=0;
int j=0;
int[] next = new int[l2+1];
getNext(needle,next);
while(i<l1&&j<l2){
if(j==-1||haystack.charAt(i)==needle.charAt(j)){
i++;
j++;
}else{
j = next[j];
}
if(j==l2){
return i-l2;
}
}
return -1;
} public void getNext(String s, int[] next){
int i,j;
int len = s.length();
i=0;
j=-1;
next[0]=-1;
while(i<len-1){
if(j==-1||s.charAt(i)==s.charAt(j)){
i++;
j++;
next[i]=j;
}else{
j=next[j];
}
}
}
}

Implement strStr()的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  8. Implement strStr() [LeetCode]

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  9. 70. Implement strStr() 与 KMP算法

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

随机推荐

  1. 打造理想的Windows 10 APP开发环境的5个步骤

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软即将发布Windows 10手机版,实际上很多人现在已经开始在开发Windows ...

  2. C++中单例模式

    //C++单例模式:指一个类只生成一个对象 #include <iostream> using namespace std; class A{ public: static A* getA ...

  3. python-logging-日志系统

    有时候需要记录日志,典型的出现在web程序或者服务器中,需要与正在运行的程序交互或者得知里面正在运行的信息 最近在倒腾webservice,使用spyne模块进行打包服务,很多实例代码也都用到了这个l ...

  4. Eclipse导出可执行Java工程/可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  5. loadrunner实现浮点型数据转换成字符串

    ftoa(float floatNum, char *convFloatString) { char new[10]; float number,dTemp,temp_val; int base, f ...

  6. ViewPager的广告条轮播

    首先布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  7. python 继承

    继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...

  8. Java在ACM中的应用

    Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...

  9. Python学习笔记02

      元组:圆括号的,不能进行赋值操作,即不可更改. 列表:方括号的,可以修改. 访问:均使用下标访问   # 元组是一个静态列表,初始化之后就不可以修改,可以试任意类型 tuple1 = ('a st ...

  10. 《Getting Started with Storm》译文 Homepage

    拿到这本书感觉还挺薄,所以当下就想赶紧读完,然后尝试着翻译下,并加上一些自己的理解,作学习交流之用,非盈利性质 这段时间在做一个  分布式的.支持大吞吐的.实时的日志系统 ,主要用到的开源方案有Kaf ...