当前位置:网站首页>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
}
}
}
}
边栏推荐
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- Web server: how to choose a good web server these five aspects should be paid attention to
- How to view the changes and opportunities in the construction of smart cities?
- STM32 光敏电阻传感器&两路AD采集
- Ten wastes of software research and development: the other side of research and development efficiency
- Common UNIX Operation and maintenance commands of shell
- RDF query language SPARQL
- 2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
- VR线上展览所具备应用及特色
- 1. Mobile terminal touch screen event
猜你喜欢

Maixll dock quick start

Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition

2022年T电梯修理题库及模拟考试

分布式全局唯一ID解决方案详解

C -- array

2022年化工自动化控制仪表操作证考试题库及答案

Pytorch(二) —— 激活函数、损失函数及其梯度

2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试

Simple implementation of slf4j

神经网络-最大池化的使用
随机推荐
The index is invalid
Shell analysis server log command collection
Dede collection plug-in does not need to write rules
STM32 光敏电阻传感器&两路AD采集
1. Mobile terminal touch screen event
How to do the performance pressure test of "Health Code"
Codeforces Round #771 (Div. 2) ABCD|E
STM32扩展板 数码管显示
2022-02-15 (399. Division evaluation)
js解决浮点数相乘精度丢失问题
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
How to use maixll dock
Section 27 remote access virtual private network workflow and experimental demonstration
pytorch中常用数据集的使用方法
TCP server communication flow
Why is Hong Kong server most suitable for overseas website construction
C language games (I) -- guessing games
分布式锁的实现
Basic usage, principle and details of session