当前位置:网站首页>LeetCode_ 28 (implement strstr())
LeetCode_ 28 (implement strstr())
2022-07-01 04:44:00 【***】
Title Description :
Here are two strings haystack and needle , Please come in haystack Find in string needle The first place the string appears ( Subscript from 0 Start ). If it doesn't exist , Then return to -1 .
explain :
When needle When it's an empty string , What value should we return ? This is a good question in an interview .
For this question , When needle When it's an empty string, we should return 0 . This is related to C Linguistic strstr() as well as Java Of indexOf() The definition matches .
Example 1:
Input :haystack = “hello”, needle = “ll”
Output :2
Example 2:
Input :haystack = “aaaaa”, needle = “bba”
Output :-1
Tips :
1 <= haystack.length, needle.length <= 104
haystack and needle It only consists of lowercase English characters
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(); // Convert substrings into character arrays for easy calculation next Array
int[] next=new int[subc.length]; // Statement next Array
getNext(subc,next); // Calculation next Array
int i=0,j=0; // Double pointer traverses main string and substring
while(i<str.length()&&j<subs.length()){
if(j==-1||str.charAt(i)==subs.charAt(j)){
i++;j++;
}
else{
j=next[j]; // Substring backtracking
}
if(j==subs.length()){
return i-j; // return index Subscript
}
}
return -1;
}
public void getNext(char[] subc,int[] next){
next[0]=-1; // Set the first bit of the array to -1
int i=1; // Subscript from substring to 2 Start to calculate the position of
int j=-1; // here j by next[0] Value
while(i<subc.length){
// Traversal substring
if(j==-1||subc[i-1]==subc[j]){
// Judge whether the characters after the last longest public prefix are equal
next[i++]=++j; // Maximum common prefix length plus one
}
else{
j=next[j]; // Recursively find the longest public prefix forward
}
}
}
}
边栏推荐
- LeetCode_66(加一)
- 无器械健身
- Research on medical knowledge atlas question answering system (I)
- PgSQL failed to start after installation
- 2022年T电梯修理题库及模拟考试
- Shell之一键自动部署Redis任意版本
- LeetCode_28(实现 strStr())
- Summary of testing experience - Testing Theory
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
- How to do the performance pressure test of "Health Code"
猜你喜欢

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

The index is invalid

Maixll-Dock 快速上手

One click shell to automatically deploy any version of redis

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

STM32扩展板 温度传感器和温湿度传感器的使用

C#读写应用程序配置文件App.exe.config,并在界面上显示

Pytest automated testing - compare robotframework framework

This sideline workload is small, 10-15k, free unlimited massage

2022 hoisting machinery command registration examination and hoisting machinery command examination registration
随机推荐
Pytorch(一) —— 基本语法
洗个冷水澡吧
How to do the performance pressure test of "Health Code"
About the transmission pipeline of stage in spark
[2020 overview] overview of link prediction based on knowledge map embedding
Talk about testdeploy
Ten wastes of software research and development: the other side of research and development efficiency
Daily question - line 10
Common UNIX Operation and maintenance commands of shell
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
科研狗可能需要的一些工具
This sideline workload is small, 10-15k, free unlimited massage
数据加载及预处理
LeetCode_53(最大子数组和)
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
常用的Transforms中的方法
LeetCode_58(最后一个单词的长度)
解决qiankun中子应用外链文件无法获取