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

边栏推荐
- 孙老师版本JDBC(2022年6月12日21:34:25)
- Feitian +cipu body brings more imagination to the metauniverse
- How to install mysql8.0 database under Windows system? (Graphic tutorial)
- 两个文件 合并为第三个文件 。
- 【最详细】最新最全Redis面试大全(70道)
- C: 反转链表
- 证券开户安全吗,有没有什么危险呢
- Tiktok practice ~ search page ~ scan QR code
- 股票开户的具体步骤是什么?网上开户安全吗?
- C: Reverse linked list
猜你喜欢

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

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

Flutter TextField详解

How to install mysql8.0 database under Windows system? (Graphic tutorial)

【山东大学】考研初试复试资料分享

Keep alive cache component in Vue

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

Super VRT

回溯思路详解

IDEA 报错:Process terminated【已解决】
随机推荐
Some cold knowledge about QT database development
JWT操作工具类分享
Flutter TextField详解
这些地区考研太疯狂!哪个地区报考人数最多?
Tiktok practice ~ homepage video ~ pull-down refresh
mysql存储过程
手机股票注册开户有没有什么风险?安全吗?
leetcode刷题:字符串02( 反转字符串II)
C primer plus learning notes - 3. Character IO (input / output)
剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
与 MySQL 建立连接
Sword finger offer II 091 Paint the house
Is it safe to open an account for CICC Wealth Online?
Idea error: process terminated
Uni app uses canvas to draw QR code
孙老师版本JDBC(2022年6月12日21:34:25)
Daily basic use of alicloud personal image warehouse
Swagger: how to generate beautiful static document description pages
证券开户安全吗,有没有什么危险呢
【贝叶斯分类4】贝叶斯网