实现 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. mac下无法远程桌面连接win10的解决办法

    原文链接:http://www.hangge.com/blog/cache/detail_899.html 原来在Mac OSX下远程win7系统很正常,后来把windows系统升级成了win10,再 ...

  2. Poj 2403 Hay Points(Map)

    一.题目大意 实现一个工资计算系统.工资的计算规则是:首先,给定一些关键字和对应的价值,这个相对于字典.然后给出的是求职者的描述,如果这个描述中包含关键字则加上对应的价值,总得价值就是这个求职者的工资 ...

  3. JVM体系结构之二:类加载器

    一.概述 定义:虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型.类加载和连接的过程都是在运行期间完成的. 二. 类的加载 ...

  4. ASP.NET MVC 3:缓存功能的设计问题

    今天这一篇文章我来谈一谈在MVC 3项目中的缓存功能,以及针对缓存的一些设计上的考量,给大家参考参考. 为什么需要讨论缓存?缓存是一个中大型系统所必须考虑的问题.为了避免每次请求都去访问后台的资源(例 ...

  5. ansible应用案例-一键安装flask

    一.添加主机 sudo vim /etc/ansible/hosts ------------------------------------------------------> [group ...

  6. mysql四个默认数据库

    1.Master数据库  Master数据库记录了Sqlserver所有的服务器级系统信息,所有的注册帐户和密码,以及所有的系统设置信息,还记录了所有用户定义数据库的存储位置和初始化信息. 2.Tem ...

  7. [Uva12260]Free Goodies(dp+贪心)

    解题关键:先对p进行排序,消除p的影响,然后对w进行01背包即可.注意p对w的约束.j<=(cur+1)/2 #include<cstdio> #include<cstring ...

  8. [原创]Javascript 利用mousetrap.js进行键盘事件操作

    我们日常开发中,会遇到js的键盘操作,例如回车提交表单之类的.或者按下某个键执行某个方法.无意中发现一个大小不到4K的js文件,它非常方便的操作键盘事件. 自己也尝试了一下:具体代码如下: 详情可以去 ...

  9. Python学习——输入和输出

    (转自:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316434841 ...

  10. CodeForces 492E Vanya and Field (思维题)

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...