乘风破浪: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()的更多相关文章

  1. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

  2. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  3. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  4. 乘风破浪:LeetCode真题_038_Count and Say

    乘风破浪:LeetCode真题_038_Count and Say 一.前言     这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...

  5. 乘风破浪:LeetCode真题_037_Sudoku Solver

    乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决     这道题 ...

  6. 乘风破浪:LeetCode真题_036_Valid Sudoku

    乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...

  7. 乘风破浪:LeetCode真题_035_Search Insert Position

    乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...

  8. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  9. 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array

    乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言     将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...

随机推荐

  1. 记一次pm2的踩坑

    1.问题: 公司采用了自动发布平台,最近突然发现一个问题,上线完成后服务是能正常访问的,但是有一个节点访问的时候每两次中总是有一次404,通过nginx的access日志分析发现第一次正常访问有一次g ...

  2. angular ng-repeat元素swiper无法滑动问题解决

    前言 angular中ng-repeat元素swiper无法滑动,angular与swiper冲突. 1.问题 在项目中,我需要利用ng-repeat循环li,比如一个nav导航条,在加入swiper ...

  3. 跨站点请求伪造(CSRF)

    一.前言 跨站点请求伪造(Cross-SiteRequest Forgeries, CSRF),是指攻击者通过设置好的陷阱,强制对已完成认证的用户进行非预期的个人信息或设定信息等某些状态更新,属于被动 ...

  4. 阿里云服务器搭建SVN

    简单步骤介绍 1:安装svn apt-get install subversion 2. 开启svn服务器 svnserve -d 检查是否开启:ps aux | grep svnserve 若出现如 ...

  5. Quart2D图形上下文

    学习了下绘制文本图形后,下面学习图形上下文栈. 在Quart 2D绘制简单图形http://www.cnblogs.com/cuiyw/p/4401857.html时,如果绘制不一样属性的内容时,在渲 ...

  6. bootstrap轮播图

    <!doctype html><html><head> <meta charset="utf-8"> <title>使用 ...

  7. AutoMapper之投影

    7.投影 AutoMapper有一种自定义映射,叫投影.接下来我们通过一个示例来了解它 7.1示例 //源对象 public class CalendarEvent { public DateTime ...

  8. 【git1】git+gitHub+webStorm的使用

    Git与GitHub什么关系? git:版本控制工具/系统. github:是一个用git控制的项目托管平台.(同样,码云和Gogs也是基于git的项目托管平台.) ...此文接下来讲的是怎么搭配Gi ...

  9. Linux常用基本命令( touch )

    touch命令: 作用:创建空文件,或者改变文件的时间戳属性 格式: touch [option] [file] 1,同时创建一个或者多个空文件 ghostwu@dev:~/linux/mkdir$ ...

  10. js-ES6学习笔记-Generator函数

    1.Generator 函数是 ES6 提供的一种异步编程解决方案.形式上,Generator 函数是一个普通函数,但是有两个特征.一是,function关键字与函数名之间有一个星号:二是,函数体内部 ...