当前位置:网站首页>28. Implement strstr() implement strstr()
28. Implement strstr() implement strstr()
2022-07-26 06:43:00 【DXB2021】
Implement strStr().
Realization strStr() function .
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.
Here are two strings haystack and needle , Please come in haystack Find in string needle The first place the string appears ( Subscript from 0 Start ). If it doesn't exist , Then return to -1 .
Clarification:
explain :
What should we return when needle is an empty string? This is a great question to ask during an interview.
When needle When it's an empty string , What value should we return ? This is a good question in an interview .
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
For this question , When needle When it's an empty string, we should return 0 . This is related to C Linguistic strstr() as well as Java Of indexOf() The definition matches .
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 1:
Input :haystack = "hello", needle = "ll"
Output :2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Example 2:
Input :haystack = "aaaaa", needle = "bba"
Output :-1
Constraints:
1 <= haystack.length, needle.length <= 
haystack and needle consist of only lowercase English characters.
Tips :
1 <= haystack.length, needle.length <= 
haystack and needle It only consists of lowercase English characters
C Language :
int strStr(char * haystack, char * needle){
int h=0,n=0;
for(h=0;;h++)
{
if(haystack[h]=='\0')
break;
}
for(n=0;;n++)
{
if(needle[n]=='\0')
break;
}
for(int i=0;i+n<=h;i++)
{
bool flag=true;
for(int j=0;j<n;j++)
{
if(haystack[i+j]!=needle[j])
{
flag=false;
break;
}
}
if(flag)
{
return i;
}
}
return -1;
}Execution results : adopt
Execution time :0 ms, In all C Defeated in submission 100.00% Users of
Memory consumption :5.6 MB, In all C Defeated in submission 42.55% Users of
Pass the test case :75 / 75
边栏推荐
- A tool for quickly switching local host files -- switchhosts
- Why use the static keyword when defining methods
- Advanced C language - archived address book (file)
- 『HarmonyOS』DevEco的下载安装与开发环境搭建
- Address resolution ARP Protocol
- 『牛客|每日一题』有效括号序列
- Conda 虚拟环境envs目录为空
- What is KVM? What is KVM virtual machine?
- Show you the principle of IO multiplexing (select, poll and epoll)
- Design principle of infrared circuit of single chip microcomputer
猜你喜欢

『HarmonyOS』DevEco的下载安装与开发环境搭建

IP day 10 notes - BGP

Design principle of infrared circuit of single chip microcomputer

TCP protocol -- message format, connection establishment, reliable transmission, congestion control

MySQL Foundation (II) -- MySQL Foundation

XSS-labs(1-10)闯关详解

Can C use reflection to assign values to read-only attributes?

SQL optimization scheme

曲线曲率展示

"Niuke | daily question" inverse Polish expression
随机推荐
Download, installation and development environment construction of "harmonyos" deveco
7. Reverse Integer整数反转
"Niuke | daily question" inverse Polish expression
[1]数学建模基础入门知识
[C language] address book dynamic version and document version
The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
MySQL optimized index and index invalidation
Heap sort
打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 &lt;serviceDebug&gt; 配置行为)以便将异常信息发送回
Gdown Access denied:Cannot retrieve the public link of the file.
Deep learning - CV, CNN, RNN
Can C use reflection to assign values to read-only attributes?
How strong is the working ability of a project manager with a monthly salary of 50000?
The real epidemic situation in the United States, do not easily "bottom" 2020-03-23
C# 可以利用反射给只读属性赋值吗?
TCP protocol -- message format, connection establishment, reliable transmission, congestion control
What is the concept and purpose of white box testing? And what are the main methods?
归并排序(merge_sort)
Gdown Access denied:Cannot retrieve the public link of the file.
Upgrade appium automation framework to the latest 2.0