实现 strStr()。
返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 。
例 1:
输入: haystack = "hello", needle = "ll"
输出: 2
例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
详见:https://leetcode.com/problems/implement-strstr/description/

Java实现:

方法一:暴力解

class Solution {
public int strStr(String haystack, String needle) {
int hsize=haystack.length();
int nsize=needle.length();
int i=0;
int j=0;
while(i<hsize&&j<nsize){
if(haystack.charAt(i)==needle.charAt(j)){
++i;
++j;
}else{
i=i-j+1;
j=0;
}
}
if(j==nsize){
return i-j;
}else{
return -1;
}
}
}

方法二:KMP算法

class Solution {
public int strStr(String s, String p) {
int i=0;
int j=-1;
int ps=p.length();
int[] next=new int[ps+1];
next[0]=-1;
while(i<ps){
if(j==-1||p.charAt(i)==p.charAt(j)){
++i;
++j;
next[i]=j;
}else{
j=next[j];
}
}
int ss=s.length();
i=0;
j=0;
while(i<ss&&j<ps){
if(j==-1||s.charAt(i)==p.charAt(j)){
++i;
++j;
}else{
j=next[j];
}
}
if(j==ps){
return i-j;
}
return -1;
}
}

028 Implement strStr() 实现 strStr()的更多相关文章

  1. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

  2. Java for LeetCode 028 Implement strStr()

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

  3. 【LeetCode】028. Implement strStr()

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

  4. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

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

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

  6. [leetcode]28. Implement strStr()实现strStr()

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

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

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

  8. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

  9. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...

随机推荐

  1. POJ2513(字典树+图的连通性判断)

    //用map映射TLE,字典树就AC了#include"cstdio" #include"set" using namespace std; ; ;//26个小 ...

  2. 报错之-Cannot set property 'onclick' of null

    当js文件放在head里面时,如果绑定了onclick或者onmouseover事件,就会出现如下图类似的错误,是因为浏览器的加载你写的html文档的顺序是从上往下,加载完按钮节点才执行的js,所以当 ...

  3. 微服务理论之六:ESB与SOA的关系

    一.SOA和ESB一直是没有明确概念的两个缩略词 SOA----面向服务架构,实际上强调的是软件的一种架构,一种支撑软件运行的相对稳定的结构,表面含义如此,其实SOA是一种通过服务整合来解决系统集成的 ...

  4. C#自定义控件 ————进度条

    先看看样式 一个扇形的进度条 对外公开的方法和属性 事件 value_change;//值改变时触发的事件progress_finshed;//进度条跑完时触发的事件 属性 Max_value//获取 ...

  5. centos6 搭建IPSEC

    http://www.maxwhale.com/how-to-install-l2tp-vpn-on-centos/ http://blog.earth-works.com/2013/02/22/ho ...

  6. how to download a file with Nodejs(without using third-party libraries)用node下载文件

    创建HTTP GET请求并将其管理response到可写文件流中: var http = require('http'); var fs = require('fs'); var file = fs. ...

  7. Luogu 3312 [SDOI2014]数表

    在这一篇里把所有的套路写全方便自己之后复习. 首先是一个小学生数学:$a$整除$b$ $ = $  $\frac{b}{a}$ 也就是说这题中格子$(i, j)$的值就是既能被$i$整除又能被$j$整 ...

  8. 怀旧系列(1)----FBasic

    小时候,老爸斥巨资给我买了一台小霸王学习机.玩遍了所有游戏后,里面有个F-Basic语言,黑乎乎的,一点也不好玩.直到杰兄从学校带回一本BASIC语言,才知道这玩意儿还可以编辑**图案.由于没有人指导 ...

  9. 第四周作业-视频学习、教材作业wireshark

    教材总结与作业 总结 网络嗅探技术定义:网络嗅探(sniff)是一种黑客常用的窃听技术,(1)利用计算机的网络接口截获目的地为其他计算机的数据报文,(2)监听数据流中所包含的用户账户密码或私密通信等. ...

  10. DATASET()用法

    DataSet是ADO.NET的中心概念.可以把DataSet当成内存中的数据库,DataSet是不依赖于数据库的独立数据集合.所谓独立,就是说,即使断开数据链路,或者关闭数据库,DataSet依然是 ...