当前位置:网站首页>LeetCode每日一题:实现strStr()
LeetCode每日一题:实现strStr()
2022-06-28 23:57:00 【利刃Cc】

链接: 实现strStr()
这道题有点像实现函数strcmp()差不多,只不过这里有个难点,就是如果haystack中有多个字符和与needle中的第一个字符相同,并且要是这前几个相同的字符后的字符串与needle不相同,则要重新判断。
所以这里的思路是用四个指针,其中两个是函数的形参,所以我们要再创建两个指针,分别指向haystack和needle。
//若为空直接返回0
if (haystack == NULL)
return 0;
//求出两个字符串的长度
int haystacklen = strlen(haystack);
int needlelen = strlen(needle);
//needle的长度大于haystack,则直接返回-1
if(haystacklen < needlelen)
return -1;
//开辟两个指针
char* ph = haystack;
char* pn = needle;
接着就是进入循环的部分:
对haystack的长度进行循环,然后判断*ph是否等于 needle的第一个字符,若不相等则直接 ph向后走一步。
若两个字符相等,则先保留住这个ph的位置,以防等会判断两个字符串不一致时候,返回时找不到原位置,所以 把haystack移到ph的位置。接着开始 对pn的长度循环,若两个 ph和 pn在每个位移的字符都相等,则退出循环后直接返回 i 的值,若不相等了就将 ph移到原来haystack的位置,然后 pn回到needle的位置,然后 break出来,继续循环,直到长度结束。
int flag = 1;
int i = 0;
for(i = 0; i < haystacklen; i++)
{
if(*ph != *pn)
{
ph++;
}
else
{
haystack = ph;
for(int j = 0; j < needlelen; j++)
{
if(*ph == *pn)
{
ph++;
pn++;
flag = 0;
}
else
{
ph = haystack;
ph++;
pn = needle;
flag = 1;
break;
}
}
if(flag == 0)
return i;
}
}
return -1;
完整的代码:
int strStr(char * haystack, char * needle){
if (haystack == NULL)
return 0;
//求出两个字符串的长度
int haystacklen = strlen(haystack);
int needlelen = strlen(needle);
//needle的长度大于haystack,则直接返回-1
if(haystacklen < needlelen)
return -1;
//开辟两个指针
char* ph = haystack;
char* pn = needle;
int flag = 1;
int i = 0;
for(i = 0; i < haystacklen; i++)
{
if(*ph != *pn)
{
ph++;
}
else
{
haystack = ph;
for(int j = 0; j < needlelen; j++)
{
if(*ph == *pn)
{
ph++;
pn++;
flag = 0;
}
else
{
ph = haystack;
ph++;
pn = needle;
flag = 1;
break;
}
}
if(flag == 0)
return i;
}
}
return -1;
}

边栏推荐
- MapReduce case
- Trois questions PWN
- Save data in Excel: use openpyxl to create multiple tables and set excel row limit
- [C Primer Plus Chapter II after class programming questions]
- Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
- 入行数字IC验证后会做些什么?
- Sword finger offer 12 Path in matrix
- What will be done after digital IC Verification?
- Form verification problem - El select (solution to automatically trigger verification on initialization page)
- Auto encoder
猜你喜欢

TypeScript -- 第二节:变量声明

转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)

stm32F407-------串行(串口)通信

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
![[opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering](/img/1d/3a46517cbfa90005a15d7858d81ca9.png)
[opencv] - linear filtering: box filtering, mean filtering, Gaussian filtering

stm32F407-------电容触摸按键

Typescript -- Section 3: Interface

LinkedIn datahub - experience sharing

MapReduce case

ERROR 1067 (42000): Invalid default value for ‘end_ time‘ Mysql
随机推荐
Form verification problem - El select (solution to automatically trigger verification on initialization page)
小白创业做电商,选对商城系统很重要!
Typescript -- Section 5: classes
PHP function file_ get_ Contents and memory mapping of operating system
Stm32f407 ------- IO pin multiplexing mapping
MapReduce case
Is it safe to open an account for buying stocks online?
Phoenix安装教程
Scrapy使用xlwt实现将数据以Excel格式导出的Exporter
Learning fuzzy from SQL injection to bypass the latest safe dog WAF
stm32F407-------GPIO输入实验
Notes: three ways to define setters and Getters
Test experience: how testers evolve from 0 to 1
Picture 64base transcoding and decoding
stm32F407-------RTC实时时钟
Along with the notes: methods simulating array like classes
TypeScript -- 第七节 枚举
Typescript -- Section 1: basic types
三個pwn題
6.28 learning content