当前位置:网站首页>[leetcode] 28. Implement strstr ()
[leetcode] 28. Implement strstr ()
2022-07-25 20:24:00 【Xiaoqu classmate】
28、 Realization strStr()
subject :
Realization strStr() function .
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
Their thinking :
This question can be used Violent match : To solve
We can make the string needle And string haystack All the lengths of are m All substrings of match once .
To reduce unnecessary matching , Every time we fail to match, we immediately stop the matching of the current substring , Continue matching for the next substring . If the current substring matches successfully , We can return the starting position of the current substring . If all substrings fail to match , Then return to −1.
Reference code :
class Solution {
public int strStr(String haystack, String needle) {
int n = haystack.length(), m = needle.length();
for (int i = 0; i + m <= n; i++) {
boolean flag = true;
for (int j = 0; j < m; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
flag = false;
break;
}
}
if (flag) {
return i;
}
}
return -1;
}
}

边栏推荐
- Do you still have certificates to participate in the open source community?
- [advanced mathematics] [5] definite integral and its application
- Arrow parquet
- test
- [tensorrt] trtexec tool to engine
- When AI encounters life and health, Huawei cloud builds three bridges for them
- DIY个人服务器(diy存储服务器)
- Difference Between Accuracy and Precision
- 毕业从事弱电3个月,我为什么会选择转行网络工程师
- String of sword finger offer question bank summary (II) (C language version)
猜你喜欢

JMeter - interface test

Vivo official website app full model UI adaptation scheme

【TensorRT】动态batch进行推理

Difference Between Accuracy and Precision
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now

推荐系统专题 | MiNet:跨域CTR预测

Advantages of network virtualization of various manufacturers

分享 25 个有用的 JS 单行代码

Jmeter——接口测试
随机推荐
Introduction and construction of consul Registration Center
Aircraft PID control (rotor flight control)
飞行器pid控制(旋翼飞控)
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
Kubernetes进阶部分学习笔记
Fanoutexchange switch code tutorial
JVM (XXIII) -- JVM runtime parameters
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
MySQL 日期【加号/+】条件筛选问题
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
Recommended system topic | Minet: cross domain CTR prediction
增加 swap 空间
Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
PreScan快速入门到精通第十九讲之PreScan执行器配置、轨迹同步及非配多个轨迹
接口请求合并的3种技巧,性能直接爆表!
使用cookie登录百度网盘(网站使用cookie)
Summarize the level of intelligent manufacturing discussion [macro understanding]
Recommended books | essentials of industrial digital transformation: methods and Practice
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
FormatDateTime说解[通俗易懂]