当前位置:网站首页>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;
}
}
边栏推荐
- Detailed explanation of stored procedures in MySQL
- Jz-062- the k-th node of binary search tree
- Establish a connection with MySQL
- 0 basic C language (0)
- C exercise. Class list plus records, display records and clear records
- 大家都能看得懂的源码(一)ahooks 整体架构篇
- Feitian +cipu body brings more imagination to the metauniverse
- Tiktok practice ~ sharing module ~ generate short video QR code
- [recommended collection] these 8 common missing value filling skills must be mastered
- 回首望月
猜你喜欢
随机推荐
Is it safe to open an online account in case of five-year exemption?
基于QT实现简单的连连看小游戏
c语言99乘法表
股票开户的具体步骤是什么?网上开户安全吗?
网上开户万一免五到底安不安全?
leetcode刷题:字符串02( 反转字符串II)
MySQL - table creation and management
Super VRT
515. find the maximum value in each tree row
Detailed explanation of shutter textfield
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
C语言 文件光标 fseek
Unity——Mathf. Similarities and differences between atan and atan2
Stringutils judge whether the string is empty
【最详细】最新最全Redis面试大全(70道)
vue中缓存组件keep-alive
Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
Uni app uses canvas to draw QR code
515. 在每个树行中找最大值
Dynamic planning 111