当前位置:网站首页>leetcode刷题:字符串06(实现 strStr())
leetcode刷题:字符串06(实现 strStr())
2022-06-26 20:30:00 【涛涛英语学不进去】
28. 实现 strStr()
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = “hello”, needle = “ll”
输出: 2
示例 2:
输入: haystack = “aaaaa”, needle = “bba”
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
本来想要字符串模式匹配算法KMP算法的,结果我没写出来,害。
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/ * 实现 strStr() **/
public class StrStr {
public static void main(String[] args) {
System.out.println(strStr("bababbababbbabbaa", "" +
"abbba"));
}
/** * 串的模式匹配算法 * 除了特殊情况 * 1. 遍历大串 * 2. 遍历小串 * 当小串的当前字符和大串的一样时,比较下一位,如果一直到最后都相等,返回 * 如果中间遇到不相等的,则大串后移n位 * n为小串中下一个和首字母相同的位 * * @param haystack * @param needle * @return */
public static int strStr(String haystack, String needle) {
//判断特殊情况
if ("".equals(needle)) {
return 0;
}
if (haystack.length() < needle.length()) {
return -1;
}
int bigIndex = 0;
//串内偏移量
int offset = 0;
while (bigIndex < haystack.length()) {
while (needle.charAt(offset) == haystack.charAt(bigIndex + offset)) {
//双方后移继续比较
if (offset == needle.length() - 1) {
return bigIndex;
} else {
offset++;
}
//大串结束
if (bigIndex + offset > haystack.length() - 1) {
break;
}
}
bigIndex++;
offset = 0;
}
return -1;
}
}

边栏推荐
- Review of watermelon book (VII): Bayesian classifier (manual push + code demo)
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- 慕课11、微服务的用户认证与授权
- 460million zongzi were sold in half a year. How big is the "imagination space" of time-honored brands?
- Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005
- [MySQL series] collection of common working SQL (continuous update)
- 回溯思路详解
- 股票开户的具体步骤是什么?网上开户安全吗?
- Tiktok practice ~ search page ~ scan QR code
- 【贝叶斯分类3】半朴素贝叶斯分类器
猜你喜欢
随机推荐
Idea error: process terminated
Detailed explanation of stored procedures in MySQL
460million zongzi were sold in half a year. How big is the "imagination space" of time-honored brands?
710. 黑名单中的随机数
Database SQL statement writing
ImageView, glide load long picture (glide load picture)
The goal you specified requires a project to execute but there is no POM
[recommended collection] these 8 common missing value filling skills must be mastered
Boot indicator monitoring
uni-app使用canvas绘制二维码
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
Invocation failed Unexpected end of file from server
飞天+CIPU体为元宇宙带来更大想象空间
vue中缓存组件keep-alive
Developer survey: rust/postgresql is the most popular, and PHP salary is low
Stringutils judge whether the string is empty
【山东大学】考研初试复试资料分享
论数据库的传统与未来之争之溯源溯本----AWS系列专栏
关于不等式取值转义的思路
Six necessary threat tracking tools for threat hunters









