28. 实现 strStr()

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。

示例 1:

输入: haystack = “hello”, needle = “ll”

输出: 2

示例 2:

输入: haystack = “aaaaa”, needle = “bba”

输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/implement-strstr

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

PS:KMP详细说明(链接)

class Solution {
public int strStr(String S, String T) {
if (T == null || T.length() == 0) return 0;
int[] next = new int[T.length()];
getNext(T, next);
int i = 0;
int j = 0;
while (i < S.length() && j < T.length()) {
if (j == -1 || S.charAt(i) == T.charAt(j)) {
i++;
j++;
} else j = next[j];
}
if (j == T.length()) return i - j;
return -1; } private void getNext(String t, int[] next) {
next[0] = -1;
int i = 0;
int j = -1;
while (i < t.length() - 1) {
if (j == -1 || t.charAt(i) == t.charAt(j)) {
i++;
j++;
next[i] = j;
} else {
j = next[j];
}
}
}
}

Java实现 LeetCode 28 实现strStr()的更多相关文章

  1. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  2. <每日 1 OJ> -LeetCode 28. 实现 strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  3. Java for LeetCode 028 Implement strStr()

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

  4. 44. leetcode 28. Implement strStr()

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

  5. [LeetCode]28.实现strStr()(Java)

    原题地址: implement-strstr 题目描述: 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字 ...

  6. Java [leetcode 28]Implement strStr()

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

  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 28——Implement strStr()

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

  9. Leetcode 28.实现strStr() By Python

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

随机推荐

  1. JDBC11 封装+资源配置文件管理

    封装为Utils类+程序资源文件去配置 public class Utils { static Properties p=null; static { p=new Properties(); try ...

  2. linux --批量修改文件内容

    由于目前测试的BIOS有一个option 发生了改变,因此我们需要在之前写好的脚本上进行修改,将旧的option 改为新的选项,因此在此处用到了批量修改文件中的内容: 1. perl 命令替换: pe ...

  3. HttpRequestUtils post get请求

    package com.nextjoy.projects.usercenter.util.http; /** * Created by Administrator on 2016/10/20. */ ...

  4. json 格式要求

    json 格式中, 字符串类型需要使用双引号,不能为单引号

  5. 朴实的聊聊很多人会误解/不懂的Java并发中断机制

    | 好看请赞,养成习惯 你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it ...

  6. mybatis随记

    JDBC问题:1.数据库配置信息硬编码 2.频繁创建,释放数据库连接 3.sql,设置参数,获取结果集硬编码,不通用   解决方案:1.配置文件 2.采用连接池 3.使用反射和内省   自定义持久层框 ...

  7. 201771010128王玉兰《面向对象程序设计(Java)》第八周学习总结

    第一部分:理论知识部分总结 (1)接口:接口不是类,而是对类胡一组需求描述,由常量肯一组抽象方法组成. a:接口中不包括变量和有具体实现的方法 b:只要类实现了接口,则该类要遵从接口描述的统 一格式进 ...

  8. vue脚手架3.0的安装与使用

    一.安装 1.先检查是否有安装vue  (vue-cli3需要node大于等于8.9版本) //vue -V 2.如果没安装跳过.安装有3.0以下的版本就的先卸载掉以前的版本 npm uninstal ...

  9. Java——日期获取和日期格式化

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  10. 【Leetcode】560. 和为K的子数组&974. 和可被 K 整除的子数组(前缀和+哈希表)

    public class Solution { public int subarraySum(int[] nums, int k) { int count = 0, pre = 0; HashMap ...