当前位置:网站首页>LeetCode_28(实现 strStr())
LeetCode_28(实现 strStr())
2022-07-01 04:37:00 【***】
题目描述:
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。
示例 1:
输入:haystack = “hello”, needle = “ll”
输出:2
示例 2:
输入:haystack = “aaaaa”, needle = “bba”
输出:-1
提示:
1 <= haystack.length, needle.length <= 104
haystack 和 needle仅由小写英文字符组成
class Solution {
public int strStr(String str, String subs) {
if(str==null||subs==null)return -1;
if(str.length()==0||subs.length()==0)return -1;
char[] subc=subs.toCharArray(); //将子串转换成字符数组方便计算next数组
int[] next=new int[subc.length]; //声明next数组
getNext(subc,next); //计算next数组
int i=0,j=0; //双指针遍历主串和子串
while(i<str.length()&&j<subs.length()){
if(j==-1||str.charAt(i)==subs.charAt(j)){
i++;j++;
}
else{
j=next[j]; //子串回溯
}
if(j==subs.length()){
return i-j; //返回index下标
}
}
return -1;
}
public void getNext(char[] subc,int[] next){
next[0]=-1; //设置数组第一位为-1
int i=1; //从子串下标为2的位置开始计算
int j=-1; //此时j为next[0]的值
while(i<subc.length){
//遍历子串
if(j==-1||subc[i-1]==subc[j]){
//判断上一个最长公共前缀后的字符是否相等
next[i++]=++j; //最长公共前缀长度加一
}
else{
j=next[j]; //往前递归找上一个最长公共前缀
}
}
}
}
边栏推荐
- 神经网络-最大池化的使用
- Introduction to JVM stack and heap
- Grey correlation cases and codes
- RuntimeError: “max_pool2d“ not implemented for ‘Long‘
- TASK04|數理統計
- Advanced application of ES6 modular and asynchronous programming
- [2020 overview] overview of link prediction based on knowledge map embedding
- 2022 polymerization process test questions and simulation test
- 神经网络的基本骨架-nn.Moudle的使用
- Section 27 remote access virtual private network workflow and experimental demonstration
猜你喜欢

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis

OdeInt与GPU

Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022

2022危险化学品生产单位安全生产管理人员题库及答案

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

Basic usage, principle and details of session

2022 gas examination question bank and online simulation examination

Openresty rewrites the location of 302

神经网络-非线性激活
随机推荐
Applications and features of VR online exhibition
如何看待智慧城市建设中的改变和机遇?
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
神经网络-卷积层
Offline installation of Wireshark 2.6.10
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
Maixll-Dock 使用方法
Obtain detailed ideas for ABCDEF questions of 2022 American Games
slf4j 简单实现
2022 question bank and answers for safety production management personnel of hazardous chemical production units
JS image path conversion Base64 format
Difference between cookie and session
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
Task04 mathematical statistics
【LeetCode】100. Same tree
VIM easy to use tutorial
Advanced application of ES6 modular and asynchronous programming
Pytest automated testing - compare robotframework framework