当前位置:网站首页>Leetcode question brushing: String 06 (implement strstr())
Leetcode question brushing: String 06 (implement strstr())
2022-06-26 20:50:00 【Taotao can't learn English】
28. Realization strStr()
Realization strStr() function .
Given a haystack String and a needle character string , stay haystack Find in string needle The first place the string appears ( from 0 Start ). If it doesn't exist , Then return to -1.
Example 1:
Input : haystack = “hello”, needle = “ll”
Output : 2
Example 2:
Input : haystack = “aaaaa”, needle = “bba”
Output : -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 .
Originally, I wanted a string pattern matching algorithm KMP Algorithm , I didn't write it , harm .
package com.programmercarl.string;
/** * @ClassName StrStr * @Descriotion TODO * @Author nitaotao * @Date 2022/6/25 15:52 * @Version 1.0 * https://leetcode.cn/problems/implement-strstr/ * Realization strStr() **/
public class StrStr {
public static void main(String[] args) {
System.out.println(strStr("bababbababbbabbaa", "" +
"abbba"));
}
/** * String pattern matching algorithm * Except in special cases * 1. Traverse a large string * 2. Traversal string * When the current character of the small string is the same as that of the large string , Compare next , If they are equal all the way to the end , return * If you encounter unequal , Then the big string moves back n position * n For the next bit in the string that is the same as the initial letter * * @param haystack * @param needle * @return */
public static int strStr(String haystack, String needle) {
// Judge special cases
if ("".equals(needle)) {
return 0;
}
if (haystack.length() < needle.length()) {
return -1;
}
int bigIndex = 0;
// In string offset
int offset = 0;
while (bigIndex < haystack.length()) {
while (needle.charAt(offset) == haystack.charAt(bigIndex + offset)) {
// Both sides move back and continue to compare
if (offset == needle.length() - 1) {
return bigIndex;
} else {
offset++;
}
// The string ends
if (bigIndex + offset > haystack.length() - 1) {
break;
}
}
bigIndex++;
offset = 0;
}
return -1;
}
}

边栏推荐
猜你喜欢

Muke 11. User authentication and authorization of microservices

Feitian +cipu body brings more imagination to the metauniverse

The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?

MySQL - table creation and management

Super VRT

Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)

Some cold knowledge about QT database development

大家都能看得懂的源码(一)ahooks 整体架构篇

好物推薦:移動端開發安全工具
![[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)](/img/a8/622cddae2ac8c383979ed51d36bca9.jpg)
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
随机推荐
Gd32 USB composite device file descriptor
C语言 文件光标 fseek
Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
孙老师版本JDBC(2022年6月12日21:34:25)
案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
Is there any risk in opening a mobile stock registration account? Is it safe?
C# 练习。类列表加记录,显示记录和清空记录
Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)
Unity - URP get camera stack
【最详细】最新最全Redis面试大全(42道)
大家都能看得懂的源码(一)ahooks 整体架构篇
剑指 Offer II 091. 粉刷房子
云计算技术的发展与芯片处理器的关系
c语言简单的登录
超分之VRT
Tiktok practice ~ sharing module ~ short video download (save to photo album)
网上开户万一免五到底安不安全?
JWT操作工具类分享
515. find the maximum value in each tree row
Jz-062- the k-th node of binary search tree