28. Find the Index of the First Occurrence in a string
创始人
2025-05-30 23:59:36
0

The Description of the problem

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = “sadbutsad”, needle = “sad”
Output: 0
Explanation: “sad” occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.

Example 2:

Input: haystack = “leetcode”, needle = “leeto”
Output: -1
Explanation: “leeto” did not occur in “leetcode”, so we return -1.

The codes in Python

class Solution:def strStr(self, haystack: str, needle: str) -> int:if len(needle) == 0:return 0# get next arraynext = [0]pos = 0for i in range(1, len(needle)):while pos != 0 and needle[pos] != needle[i]:pos = next[pos - 1]if needle[pos] == needle[i]:pos += 1next.append(pos)# find the index of the first occurrence of needle in haystackj = 0for i in range(len(haystack)):while j >= 1 and haystack[i] != needle[j]:j = next[j - 1]if haystack[i[ == needle[j]:j += 1if j == len(needle):return i - j + 1return -1

The codes in C++

The codes with a bug (Can you find it and fix it)

class Solution {
public:int strStr(string haystack, string needle) {if (needle.size() == 0) {return 0;}// get the next arrayvector next = {0};int pos = 0;for (int i = 1; i < needle.size(); i++) {while (pos > 0 && needle[pos] != needle[i]) {pos = needle[pos - 1];}if (needle[pos] == needle[i]) {pos++;}next.push_back(pos);}// find the index of the first occurrence of needle in haystackint j = 0;for (int i = 0; i < haystack.size(); i++) {while (j > 0 && haystack[i] != needle[j]) {j = next[j - 1];}if (haystack[i] == needle[j]) {j++;}if (j == needle.size()) {return i - needle.size() + 1;}}return -1;}
};

The C++ codes with bug free

class Solution {
public:int strStr(string haystack, string needle) {if (needle.size() == 0) {return 0;}// get the next arrayvector next = {0};int pos = 0;for (int i = 1; i < needle.size(); i++) {while (pos > 0 && needle[pos] != needle[i]) {pos = next[pos - 1];}if (needle[pos] == needle[i]) {pos++;}next.push_back(pos);}// find the index of the first occurrence of needle in haystackint j = 0;for (int i = 0; i < haystack.size(); i++) {while (j > 0 && haystack[i] != needle[j]) {j = next[j - 1];}if (haystack[i] == needle[j]) {j++;}if (j == needle.size()) {return i - needle.size() + 1;}}return -1;}
};

相关内容

热门资讯

【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
牛客计算器的改良(Python... 文章目录1.题目描述2.输入描述:3.输出描述:4.示例15.分析6.代码7.结语 链接࿱...
【前端】‘??‘与‘||‘有什... 0 问题 经常写const data = res.data.a ?? ''或者const d...
正大杯|市调大赛|2023备赛... 关键信息 同时随着精细化养宠趋势的深入,宠物消费类目日渐丰富。 本报告通过 Niuco...
文本生成视频Make-A-Vi... Meta公司(原Facebook)在今年9月29日首次推出一款人工智能系...