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

边栏推荐
- Use menu resources to implement option menus and context menus
- Stm32f407-------- NVIC interrupt priority management
- websocket-js连接如何携带token验证
- Is it reliable and safe to avoid five in case of stock trading account opening
- [C Prime plus chapitre II Questions de programmation après la Classe]
- 随笔记:定义setter和getter的三种方式
- What pitfalls should be avoided in the job interview for the operation post in 2022?
- Yes, use local_ setup. Bash or setup bash
- 10. Standard i/o redirection and pipeline
- stm32F407-------NVIC中断优先级管理
猜你喜欢

Notes: three ways to define setters and Getters

一条update语句到底加了多少锁?带你深入理解底层原理

Xiaobai's e-commerce business is very important to choose the right mall system!

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

stm32F407-------寄存器地址名称映射分析

TypeScript -- 第一节:基础类型

Mysql-5.7.30-winx64 installation free download and installation tutorial

Typescript -- Section 1: basic types

This thing is called a jump watch?

TypeScript--第四节:函数
随机推荐
Yyds dry goods inventory building knowledge map from scratch with neo4j (I)
三个pwn题
这玩意叫跳表?
stm32F407-------跑马灯、蜂鸣器
MapReduce case
What is the lifecycle of automated testing?
Save data in Excel: use openpyxl to create multiple tables and set excel row limit
Windows10 phpstudy installing redis extension
Solve the problem of Chinese parsing by configparser
Stm32f407----- capacitive touch button
Reading notes of English grammar new thinking Basic Edition 2 (I)
转载:VTK笔记-裁剪分割-不规则闭合圈选裁剪-vtkSelectPolyData类(黑山老妖)
Is it safe to open an account for buying stocks online?
PHP利用CURL实现登录网站后下载Excel文件
Online yaml to JSON tool
Trois questions PWN
Association line exploration, how to connect the two nodes of the flow chart
Edge extraction based on Halcon learning [2] circles Hdev routine
mysql 8.0以上报2058 解决方式
《英语语法新思维 基础版2》读书笔记(一)