实现 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. 【转】 Pro Android学习笔记(四九):ActionBar(2):Action图标区

    目录(?)[-] ActionBar的隐藏和现实 ActionBar的action图标区 ActionBar的隐藏和现实 ActionBar bar = getActionBar();bar.hide ...

  2. PowerDesigner生成CDM模型

    一.新建概念数据模型  1)选择File-->New,弹出如图所示对话框,选择CDM模型(即概念数据模型)建立模型. 2)完成概念数据模型的创建.以下图示,对当前的工作空间进行简单介绍.(以后再 ...

  3. Dialog 基本使用

    1   :  效果图 btnGeneral.setOnClickListener(new View.OnClickListener() { @Override public void onClick( ...

  4. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; che

    出现此种错误,我暂时遇到了两次. 1 我的字段的名称和数据库的关键字重合. 上图中的desc是默认降序排列的意思. 2 第二次出现的异常是我在重构代码阶段遇到的一个bug.不过我暂时不能理解,虽然解决 ...

  5. Stream接口

    数据读写可以看作是事件模式(Event)的特例,不断发送的数据块好比一个个的事件.读数据是read事件,写数据是write事件,而数据块是事件附带的信息.Node 为这类情况提供了一个特殊接口Stre ...

  6. 面试题: mysql 数据库已看 sql安全性 索引 引擎 sql优化

    总结的一些MySQL数据库面试题 2016年06月16日 11:41:18 阅读数:4950 一.sql语句应该考虑哪些安全性? (1)防止sql注入,对特殊字符进行转义,过滤或者使用预编译的sql语 ...

  7. Java核心技术 卷1 基础知识-第一天

    基本数据类型 java是一种强数据类的的语言 共有8种基本数据类型 其中: 整型4种 int(4字节) short(2字节) long(8字节) byte(1字节) java中整型的范围与机器无关 长 ...

  8. 4、在线blast比对结果解析(保守结构域)

    转载:http://www.bio1000.com/experiment/fenzi/237846.html 标签: NCBI Blast LASTP 摘要 : NCBI BLAST比对结果报告分析: ...

  9. Shell字符串截取处理文件路径

    在生信处理流程中,从最初的fastq文件,经过分析处理后,会生成一堆的后续文件,如何在流程中合理的命名呢? 通常在批处理模式中,我们会得到多个样本*.fastq(或*.fq.*.fastq.gz.*. ...

  10. Linux 静态库(.a)转换为动态库(.so)

    Linux 静态库转换为动态库 参考 http://blog.csdn.net/moxuansheng/article/details/5812410 首先将.a文件转为.so文件是可以实现的 原因是 ...