当前位置:网站首页>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
}
}
}
}
边栏推荐
- VIM简易使用教程
- 2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- Grey correlation cases and codes
- 数据加载及预处理
- STM32扩展版 按键扫描
- 【硬十宝典】——1.【基础知识】电源的分类
- Codeworks round 449 (Div. 1) C. Kodori tree template
- C language games (I) -- guessing games
- 洗个冷水澡吧
猜你喜欢

The index is invalid

RuntimeError: “max_pool2d“ not implemented for ‘Long‘

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and

slf4j 简单实现

I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own

Daily question - line 10

Software testing needs more and more talents. Why do you still not want to take this path?

测量三相永磁同步电机的交轴直轴电感

Pytest automated testing - compare robotframework framework

Introduction to JVM stack and heap
随机推荐
2022 polymerization process test questions and simulation test
How to do the performance pressure test of "Health Code"
Applications and features of VR online exhibition
Shell之分析服务器日志命令集锦
神经网络的基本骨架-nn.Moudle的使用
Difference between cookie and session
Basic exercise of test questions hexadecimal to decimal
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
Why is Hong Kong server most suitable for overseas website construction
软件研发的十大浪费:研发效能的另一面
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
Research on medical knowledge atlas question answering system (I)
Odeint and GPU
Pytorch(一) —— 基本语法
RDF query language SPARQL
Offline installation of Wireshark 2.6.10
2022-02-15 (399. Division evaluation)
STM32 extended key scan
OdeInt與GPU
Leecode record 1351 negative numbers in statistical ordered matrix