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

边栏推荐
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- 案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
- 关于Qt数据库开发的一些冷知识
- Keep alive cache component in Vue
- 30. 串联所有单词的子串
- 数据库SQL语句撰写
- swagger:如何生成漂亮的静态文档说明页
- Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
- BOM and DOM operations
- Boot的单元测试
猜你喜欢

阿里云个人镜像仓库日常基本使用

Kubernetes resource topology aware scheduling optimization

【贝叶斯分类2】朴素贝叶斯分类器
MySQL中存储过程的详细详解

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023

Unit test of boot

Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005

抖音实战~搜索页面~视频详情

Muke 11. User authentication and authorization of microservices

Dynamic planning 111
随机推荐
The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j
0 basic C language (3)
The two files are merged into a third file.
Détails de l'annotation des ressources sentinelles
GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
开户可以在网上开么?能安全吗?
抖音实战~分享模块~短视频下载(保存到相册)
【贝叶斯分类3】半朴素贝叶斯分类器
515. 在每个树行中找最大值
慕课11、微服务的用户认证与授权
0 basic C language (0)
0基础c语言(0)
mysql的充值问题
[recommended collection] these 8 common missing value filling skills must be mastered
Kubernetes resource topology aware scheduling optimization
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: -
与 MySQL 建立连接
动态规划111
Boot indicator monitoring