首页 > 其他分享 >【LeeCode】28. 找出字符串中第一个匹配项的下标

【LeeCode】28. 找出字符串中第一个匹配项的下标

时间:2023-02-19 22:01:48浏览次数:56  
标签:下标 charAt int needle 28 next LeeCode haystack String

【题目描述】

给你两个字符串 ​​haystack​​ 和 ​​needle​​ ,请你在 ​​haystack​​ 字符串中找出 ​​needle​​ 字符串的第一个匹配项的下标(下标从 0 开始)。如果 ​​needle​​ 不是 ​​haystack​​ 的一部分,则返回  ​​-1​ 

​https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/​


【示例】

【LeeCode】28. 找出字符串中第一个匹配项的下标_数组


【代码】admin

思路: 基于内部API

package com.company;
import java.util.*;

// 2023-2-19
class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}

public class Test {
public static void main(String[] args) {
new Solution().strStr( "sadbutsad", "sad"); // 输出: 0
new Solution().strStr( "leetcode", "leeto"); // 输出: -1
}
}


【代码】​​基于KMP​

package com.company;
import java.util.*;

// 2023-2-19
class Solution {
// 基于KMP算法
public int strStr(String haystack, String needle) {
int m = needle.length();
if (m == 0) return 0;
// 开辟前缀表的存储数组空间
int[] next = new int[m];
// 初始化
this.getNext(needle, next);

int j = 0;
for (int i = 0; i < haystack.length(); i++){
while (j > 0 && needle.charAt(j) != haystack.charAt(i)){
j = next[j - 1];
}
if (needle.charAt(j) == haystack.charAt(i)){
j++;
}
if (j == needle.length()){
return i - needle.length() + 1;
}
}
return -1;
}

public void getNext(String needle, int[] next) {
// 初始化
int j = 0;
next[0] = 0;

for (int i = 1; i < needle.length(); i++){
// 匹配不相等的情况, 回退到前一个数组
while (j > 0 && needle.charAt(j) != needle.charAt(i)){
j = next[j - 1];
}
// 匹配相等的情况
if (needle.charAt(j) == needle.charAt(i)){
j++;
}
// 更新next数组
next[i] = j;
}
}
}

public class Test {
public static void main(String[] args) {
new Solution().strStr( "sadbutsad", "sad"); // 输出: 0
new Solution().strStr( "leetcode", "leeto"); // 输出: -1
}
}



标签:下标,charAt,int,needle,28,next,LeeCode,haystack,String
From: https://blog.51cto.com/u_13682316/6066842

相关文章

  • 利民HR-09 2280 m.2固态散热器 - 我的硬件配置
    ......
  • 【LeeCode】剑指 Offer 58 - II. 左旋转字符串
    【题目描述】字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回......
  • 【LeeCode】151. 反转字符串中的单词
    【题目描述】给你一个字符串 ​​s​​ ,请你反转字符串中 单词 的顺序。单词 是由非空格字符组成的字符串。​​s​​ 中使用至少一个空格将字符串中的 单词 分隔开......
  • 【LeeCode】344. 反转字符串
    【题目描述】编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 ​​s​​ 的形式给出。不要给另外的数组分配额外的空间,你必须​​原地​​修改输入数......
  • 【Redis】Redis 列表 List 操作 ( 查询操作 | 根据下标获取元素 | 获取列表长度 | 增
    文章目录​​一、List列表简介​​​​二、查询操作​​​​1、根据下标获取元素​​​​2、获取指定下标索引的元素​​​​3、获取列表长度​​​​三、增操作​​​​1......
  • 【题解】U281338 分书
    自创题题目解析考虑可以先去看Sam数从标签我们可以知道,需要使用矩阵加速考虑这道题一眼看出是一道DP题(毕竟是熟悉的背包加了一个上限)再看每一种人的人数\(10^9\)!(蒙了)......
  • 【LeeCode】135. 分发糖果 -- todo
    【题目描述】​​n​​​ 个孩子站成一排。给你一个整数数组 ​​ratings​​ 表示每个孩子的评分。你需要按照以下要求,给这些孩子分发糖果:每个孩子至少分配到 ​​1​......
  • 【LeeCode】二叉树理论
    二叉树分类没有数值满⼆叉树如果⼀棵⼆叉树只有度为0的结点和度为2的结点,并且度为0的结点在同⼀层上,则这棵⼆叉树为满⼆叉树​满⼆叉树,也可以说深度为k,有2^k-1个节点的⼆叉......
  • 【LeeCode】406. 根据身高重建队列
    【题目描述】假设有打乱顺序的一群人站成一个队列,数组 ​​people​​​ 表示队列中一些人的属性(不一定按顺序)。每个 ​​people[i]=[hi,ki]​​​ 表示第 ​​i​......
  • Toyota Programming Contest 2023 Spring Qual A(AtCoder Beginner Contest 288)[A - F]
    原比赛链接A-ManyA+BProblemsA-AC代码#include<bits/stdc++.h>usingnamespacestd;intt,a,b;intread(){ intx=0,w=1; charch=getchar(); w......