Topic

Two Pointers, String

Description

https://leetcode.com/problems/implement-strstr/

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

needle /ˈniːdl/ n.针

haystack /ˈheɪstæk/ n.干草堆

a needle in a haystack:something that is impossible or extremely difficult to find, especially because the area you have to search is too large(类似中文的“大海捞针”):

Finding the piece of paper I need in this huge pile of documents is like looking for/trying to find a needle in a haystack

Link

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Example 3:

Input: haystack = "", needle = ""
Output: 0

Constraints:

  • 0 <= haystack.length, needle.length <= 5 * 104
  • haystack and needle consist of only lower-case English characters.

Analysis

方法一:双指针

方法二:KMP算法

Submission

public class ImplementStrStr {public int strStr(String haystack, String needle) {for (int i = 0;; i++) {for (int j = 0;; j++) {if (j == needle.length())return i;if (i + j == haystack.length())return -1;if (needle.charAt(j) != haystack.charAt(i + j))break;}}}private void getNext(int[] next, String s) {int j = -1;next[0] = j;for(int i = 1; i < s.length(); i++) { // 注意i从1开始while (j >= 0 && s.charAt(i) != s.charAt(j + 1)) { // 前后缀不相同了j = next[j]; // 向前回溯}if (s.charAt(i) == s.charAt(j + 1)) { // 找到相同的前后缀j++;}next[i] = j; // 将j(前缀的长度)赋给next[i]}}public int strStr2(String haystack, String needle) {if (needle.length() == 0) {return 0;}int[] next = new int[needle.length()];getNext(next, needle);int j = -1; // // 因为next数组里记录的起始位置为-1for (int i = 0; i < haystack.length(); i++) { // 注意i就从0开始while(j >= 0 && haystack.charAt(i) != needle.charAt(j + 1)) { // 不匹配j = next[j]; // j 寻找之前匹配的位置}if (haystack.charAt(i) == needle.charAt(j + 1)) { // 匹配,j和i同时向后移动 j++; }if (j == (needle.length() - 1) ) { // 文本串s里出现了模式串treturn (i - needle.length() + 1); }}return -1;}}

Test

import static org.junit.Assert.*;
import org.junit.Test;public class ImplementStrStrTest {@Testpublic void test() {ImplementStrStr obj = new ImplementStrStr();assertEquals(2, obj.strStr("hello", "ll"));assertEquals(-1, obj.strStr("aaaaaa", "bba"));assertEquals(0, obj.strStr("", ""));assertEquals(-1, obj.strStr("aaa", "aaaa"));}@Testpublic void test2() {ImplementStrStr obj = new ImplementStrStr();assertEquals(2, obj.strStr2("hello", "ll"));assertEquals(-1, obj.strStr2("aaaaaa", "bba"));assertEquals(0, obj.strStr2("", ""));assertEquals(-1, obj.strStr2("aaa", "aaaa"));}
}

LeetCode - Easy - 28. Implement strStr()相关推荐

  1. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

  2. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  3. LeetCode - 28. Implement strStr()

    28. Implement strStr() Problem's Link -------------------------------------------------------------- ...

  4. leetCode 28. Implement strStr() 字符串

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  5. LeetCode算法入门- Implement strStr() -day22

    LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...

  6. 【Leetcode】【Easy】Implement strStr()

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

  7. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  8. [leetcode] 28. Implement strStr() 解题报告

    题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...

  9. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

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

最新文章

  1. unity shader入门精要_shader入门数学基础矩阵篇
  2. VSCode中experimentalDecorators设置问题
  3. 职业生涯:怎么样学好Oracle
  4. LinQ中Skip()方法和Take()方法的使用
  5. pipeline 流水线设计
  6. thuderbird接收qq邮箱设置
  7. SpringCloud Ribbon
  8. 杭州2019年计算机技校招生,杭州电子信息职业学校2020年招生录取分数线
  9. 请问在JAVA编程中什么叫耦合?什么又叫解藕? 悬赏分:0 - 解决时间:2008-3-8 12:55...
  10. (6)FPGA面试技能提升篇(OpenCV)
  11. PHP date_sunrise,php中 date_sunrise函数具有哪些功能呢?
  12. javascript原型和原型链
  13. Linux输入子系统学习笔记
  14. 对未来国产操作系统的期望
  15. html快捷方式指定浏览器打开网页,设置桌面网址快捷方式,并默认使用指定浏览器打开...
  16. 视频教程-项目管理12个微案例-项目管理
  17. 信号量——计数信号量
  18. master主节点初始化报错 /proc/sys/net/ipv4/ip_forward contents are not set to 1
  19. 二进制部署K8S集群
  20. MA模型自协方差证明

热门文章

  1. Windows Embedded CE 6.0开发初体验(六)平台定制
  2. C语言的putpiel函数,C语言graphics.h函数介绍
  3. Oracle客户端与java_Oracle 谈 JavaFX 及 Java 客户端技术的未来
  4. 扫描路径_npj: 纳米团簇表面的自动扫描—吸附位点和扩散路径
  5. arcgis坡度结果有误或z因子前有感叹号
  6. Java swing 实现下拉框和文本框同步显示
  7. 【转】设置Win32窗口背景颜色
  8. 【转】ABP源码分析五:ABP初始化全过程
  9. 【转】一个ASP.NET MVC中ajax调用WebApi返回500 Internal Server Error的调错方法。
  10. 【转】面试:一个单例模式,足以把你秒成渣