乘风破浪:LeetCode真题_028_Implement strStr()
乘风破浪:LeetCode真题_028_Implement strStr()
一、前言
这次是字符串匹配问题,找到最开始匹配的位置,并返回。
二、Implement strStr()
2.1 问题


当模式串为空的时候,返回索引0,空字符串也是一个字符串。
2.2 分析与解决
注意到一些细节之后,我们首先想到了用笨办法来解决,其次又想到了KMP算法来匹配,需要求next数组。
笨办法比较:
class Solution {
public int strStr(String haystack, String needle) {
for(int i=0;i<= haystack.length()-needle.length();i++){
if(haystack.substring(i, i + needle.length() ).equals(needle)){
return i;
}
}
return -1;
}
}
KMP算法:
public class Solution {
/**
* 题目大意
* 实现实现strStr()函数,判断一个字符串在另一个字符串中出现的位置。如果不匹配就返回-1。
*
* 解题思路
* 使用KMP算法进行实现
*/
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null) {
return -1;
}
if (needle.length() > haystack.length()) {
return -1;
}
if ("".equals(haystack)) {
if ("".equals(needle)) {
return 0;
} else {
return -1;
}
} else {
if ("".equals(needle)) {
return 0;
}
}
return kmpIndex(haystack, needle);
}
private int kmpIndex(String haystack, String needle) {
int i = 0;
int j = 0;
int[] next = next(needle);
while (i < haystack.length() && j < needle.length()) {
if (j == -1 || haystack.charAt(i) == needle.charAt(j)) {
++i;
++j;
} else {
j = next[j];
}
}
if (j == needle.length()) {
return i - j;
} else {
return -1;
}
}
private int[] next(String needle) {
int[] next = new int[needle.length()];
next[0] = -1;
int i = 0;
int j = -1;
int k = needle.length() - 1;
while (i < k) {
if (j == -1 || needle.charAt(i) == needle.charAt(j)) {
++i;
++j;
if (needle.charAt(i) != needle.charAt(j)) {
next[i] = j;
} else {
next[i] = next[j];
}
} else {
j = next[j];
}
}
return next;
}
}
当然还有一种作弊的方法,那就是直接使用现有的方法库:
import java.lang.StringBuilder;
class Solution {
public int strStr(String haystack, String needle) { StringBuilder sb = new StringBuilder(haystack);
return sb.indexOf(needle);
}
}

三、总结
可以看到很多问题的细节非常多,其次解决问题的方法本质上来说也有优劣。对于KMP算法,我们一定要深刻的掌握,这样可以提升我们处理比较大量数据的字符串的能力。
乘风破浪:LeetCode真题_028_Implement strStr()的更多相关文章
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_038_Count and Say
乘风破浪:LeetCode真题_038_Count and Say 一.前言 这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...
- 乘风破浪:LeetCode真题_037_Sudoku Solver
乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决 这道题 ...
- 乘风破浪:LeetCode真题_036_Valid Sudoku
乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array
乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言 将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...
随机推荐
- Python web 框架之 Django 基础搭建服务
1. 需要安装 Python 和 Django 环境,Python 环境的安装我就不在多说了 2. 安装框架 Django Django 安装,推荐先装个 pip吧,easyinstall也可以,然后 ...
- ffplay源码分析7-播放控制
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10316225.html ffplay是FFmpeg工程自带的简单播放器,使用FFmpeg ...
- MySQL升5.6引发的问题
昨天项目MySQL数据库从5.5升级到5.6,导致部分表无法进行更新操作,报如下错误: When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = , updates to no ...
- UVa 10129 Play on Words(并查集+欧拉路径)
题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...
- C#语法之特性
在项目中经常可以看到在类属性上面有一个[]的东西,今天讲的东西就是它,它英文名是Attribute,中文名是特性. 一.什么是特性? 首先,我们肯定Attribute是一个类,下面是msdn文档对它的 ...
- Jquery操作属性
1.attr(name,value):修改单个属性! name :属性名称 value:属性的值 <script> $(function(){ //给div添加一个alt=hello的属性 ...
- JS DOM操作(三) Window.docunment对象——操作属性
属性:是对象的性质与对象之间关系的统称.HTML中标签可以拥有属性,属性为 HTML 元素提供附加信. 属性总是以名称/值对的形式出现,比如:name="value". 属性值始终 ...
- vb.net連接Oracle数据库
Imports Oracle.DataAccess.Client '導入命名空間 Public Class Form1 Dim Sql As String Public SqlTP As Oracle ...
- Git 入门详解
Git git核心概念详解 什么是git git是一个分布式版本控制软件,最初由林纳斯·托瓦兹创作,于2005年以GPL发布.最初目的是为更好地管理Linux内核开发而设计.应注意的是,这与GNU I ...
- 【CentOS】设置服务开机自动启动
CentOS安装好apache.mysql等服务器程序后,并没有设置成开机自动启动的,为避免重启后还要手动开启web等服务器,还是做下设置好,其实设置很简单,用chkconfig命令就行了. 例如,要 ...