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

边栏推荐
- The secondary market is full of bad news. How should the market go next? One article will show you the general trend
- Typescript -- Section 2: variable declaration
- Blue Bridge Cup top ten common heaven level skill - breath of water The type of one recursion
- PHP function file_ get_ Contents and memory mapping of operating system
- With notes: re understanding else if
- 每日一题:消失的数字
- 好用免费的PPT模板
- [Electronic Experiment 2] simple electronic doorbell
- 随笔记:定义setter和getter的三种方式
- ES6模块
猜你喜欢

The secondary market is full of bad news. How should the market go next? One article will show you the general trend

随笔记:模拟类数组(array-like)的方法

Online yaml to JSON tool

What are the virtual machine software? What are their respective roles?

【LeetCode】21. 合并两个有序链表 - Go 语言题解

stm32F407-------LCD

TypeScript -- 第二节:变量声明

The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been

Typescript-- section 4: Functions

stm32F407-------GPIO输入实验
随机推荐
TypeScript--第五节:类
单机多实例MYSQL主从复制
[buuctf.reverse] 131-135
一条update语句到底加了多少锁?带你深入理解底层原理
MapReduce case
stm32F407-------电容触摸按键
Typescript -- Section 1: basic types
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
After eight years of testing and opening experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers
Stm32f407------- general timer
stm32F407-------LCD
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
Encapsulation of JDBC connection and disconnection database
Easy to use free ppt template
How many locks are added to an update statement? Take you to understand the underlying principles
Quartz explanation and use
JVM底层又是如何实现synchronized的
MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
Xiaobai's e-commerce business is very important to choose the right mall system!
How to guarantee the delivery quality through the cloud effect test plan