当前位置:网站首页>Leetcode daily question: implementing strstr()
Leetcode daily question: implementing strstr()
2022-06-29 00:10:00 【Sharp blade CC】

link : Realization strStr()
This problem is a bit like implementing functions strcmp() almost , But there is a difficult point , Is that if haystack There are multiple characters and... In needle The first character in is the same , And if the string after the first few same characters is the same as needle inequality , Then it needs to be judged again .
So the idea here is to use four pointers , Two of them are formal parameters of the function , So we're going to create two more pointers , Point to respectively haystack and needle.
// If it is empty, it will directly return 0
if (haystack == NULL)
return 0;
// Find the length of two strings
int haystacklen = strlen(haystack);
int needlelen = strlen(needle);
//needle Is longer than haystack, Then return directly -1
if(haystacklen < needlelen)
return -1;
// Open up two pointers
char* ph = haystack;
char* pn = needle;
Then there's the part that goes into the loop :
Yes haystack The length of Cycle through , And then determine *ph Is it equal to needle First character of , If they are not equal, they are directly ph Take a step back .
If two characters are equal , Keep this first ph The location of , In case the two strings are inconsistent , The original location cannot be found when returning , therefore hold haystack Move to ph The location of . Then start Yes pn Length cycle of , If two ph and pn The characters in each shift are equal , Then exit the loop and return directly i Value , If they are not equal, they will ph Move to original haystack The location of , then pn go back to needle The location of , then break come out , Continue to cycle , Until the end of the length .
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;
Complete code :
int strStr(char * haystack, char * needle){
if (haystack == NULL)
return 0;
// Find the length of two strings
int haystacklen = strlen(haystack);
int needlelen = strlen(needle);
//needle Is longer than haystack, Then return directly -1
if(haystacklen < needlelen)
return -1;
// Open up two pointers
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;
}

边栏推荐
猜你喜欢
随机推荐
stm32F407-------电容触摸按键
EditText listening focus
stm32F407-------GPIO输入实验
畢業三年的25歲碼農總結
Common mistakes in software testing
Notes: three ways to define setters and Getters
[machine learning] numerical analysis 02 -- finding roots of arbitrary equations
The secondary market is full of bad news. How should the market go next? One article will show you the general trend
12. Détection d'objets Mask rcnn
Phoenix installation tutorial
【LeetCode】21. Merge two ordered linked lists - go language solution
Encapsulation of JDBC connection and disconnection database
Stm32f407----- capacitive touch button
Stm32f407------- external interrupt
【LeetCode】21. 合并两个有序链表 - Go 语言题解
TypeScript -- 第七节 枚举
Stm32f407 ------ serial (serial port) communication
力扣(LeetCode)178. 分数排名(2022.06.27)
MapReduce案例
TypeScript -- 第六节 泛型



![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)



