当前位置:网站首页>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
}
}
}
}
边栏推荐
- 技术分享| 融合调度中的广播功能设计
- 【硬十宝典】——1.【基础知识】电源的分类
- Question bank and answers for chemical automation control instrument operation certificate examination in 2022
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
- OdeInt与GPU
- PgSQL failed to start after installation
- C#读写应用程序配置文件App.exe.config,并在界面上显示
- CUDA development and debugging tool
- Pytorch(三) —— 函数优化
猜你喜欢

CF1638E. Colorful operations Kodori tree + differential tree array

2022年聚合工艺考试题及模拟考试

神经网络的基本骨架-nn.Moudle的使用

OdeInt與GPU

2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination

One click shell to automatically deploy any version of redis

Dataloader的使用

Extension fragment

RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding
随机推荐
扩展-Fragment
CF1638E. Colorful operations Kodori tree + differential tree array
The index is invalid
slf4j 简单实现
Leecode records the number of good segmentation of 1525 strings
Common UNIX Operation and maintenance commands of shell
2022 question bank and answers for safety production management personnel of hazardous chemical production units
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
Leecode question brushing record 1332 delete palindrome subsequence
2022年化工自动化控制仪表操作证考试题库及答案
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
洗个冷水澡吧
软件研发的十大浪费:研发效能的另一面
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
pytorch 卷积操作
数据加载及预处理
Difficulties in the development of knowledge map & the importance of building industry knowledge map
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
2022危险化学品生产单位安全生产管理人员题库及答案