当前位置:网站首页>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;
}
}

边栏推荐
- Serial port application program based on gd32
- 剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
- Sentinelresource annotation details
- Arrête d'être un bébé géant.
- The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
- 分布式ID生成系统
- C: Reverse linked list
- 阿里云个人镜像仓库日常基本使用
- [serial] shuotou O & M monitoring system 01 overview of monitoring system
- Detailed explanation of retrospective thinking
猜你喜欢

windows系統下怎麼安裝mysql8.0數據庫?(圖文教程)

MySQL - table creation and management

Muke 8. Service fault tolerance Sentinel

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?

动态规划111

leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)

Detailed explanation of retrospective thinking

好物推荐:移动端开发安全工具

分布式ID生成系统

【连载】说透运维监控系统01-监控系统概述
随机推荐
What are the specific steps for opening a stock account? Is it safe to open an account online?
Six necessary threat tracking tools for threat hunters
Tiktok practice ~ search page ~ video details
Browser event loop
Three basic backup methods of mongodb
mysql存储过程
C: Reverse linked list
Keep alive cache component in Vue
0基础学c语言(2)
西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
leetcode刷题:字符串02( 反转字符串II)
Some cold knowledge about QT database development
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
0 basic C language (1)
The source code that everyone can understand (I) the overall architecture of ahooks
Looking back at the moon
好物推薦:移動端開發安全工具
Development of NFT for digital collection platform
Invocation failed Unexpected end of file from server
【贝叶斯分类2】朴素贝叶斯分类器