当前位置:网站首页>28. Implement strStr()实现 strStr()
28. Implement strStr()实现 strStr()
2022-07-26 06:37:00 【DXB2021】
Implement strStr().
实现 strStr() 函数。
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.
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
Clarification:
说明:
What should we return when needle is an empty string? This is a great question to ask during an interview.
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
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().
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
示例 1:
输入:haystack = "hello", needle = "ll"
输出:2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
示例 2:
输入:haystack = "aaaaa", needle = "bba"
输出:-1
Constraints:
1 <= haystack.length, needle.length <= 
haystack and needle consist of only lowercase English characters.
提示:
1 <= haystack.length, needle.length <= 
haystack 和 needle 仅由小写英文字符组成
C语言:
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;
}执行结果: 通过
执行用时:0 ms, 在所有 C 提交中击败了100.00%的用户
内存消耗:5.6 MB, 在所有 C 提交中击败了42.55%的用户
通过测试用例:75 / 75
边栏推荐
- BPG notes (IV)
- 数据库中varchar和Nvarchar区别与联系
- Sorting problem: bubble sort, select sort, insert sort
- 【C语言】文件操作
- Torth file read vulnerability (cnvd-2020-27769)
- What are the main reasons for server crash
- Design principle of infrared circuit of single chip microcomputer
- Liberg avenue to Jane series
- Which "digital currencies" can survive this winter? 2020-03-30
- C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
猜你喜欢
![[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code](/img/2a/b5214e9fa206f1872293c9b9d7bdb6.png)
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code

What are the aspects of performance testing? What are the classification and testing methods?

09 eth smart contract

@Constructorproperties annotation understanding and its corresponding usage
![[1] Basic knowledge of mathematical modeling](/img/29/90b1c7533e9443852758d10080e239.png)
[1] Basic knowledge of mathematical modeling

Vision Transformer 必读系列之图像分类综述

MySQL基础篇(二)-- MySQL 基础

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

Quick sort

Facing the rebound market, how do we operate? 2020-03-21
随机推荐
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022
What is KVM? What is KVM virtual machine?
C language file operation
Esxi 7.0 installation supports mellanox technologies mt26448 [connectx en 10gige, PCIe 2.0 5gt/s] driver, and supports the cheapest 10GB dual fiber network card
Sorting problem: bubble sort, select sort, insert sort
Use scanner to get multiple data types from the keyboard
Vision Transformer 必读系列之图像分类综述
Torth file read vulnerability (cnvd-2020-27769)
What are the aspects of performance testing? What are the classification and testing methods?
Do you think you are a reliable test / development programmer? "Back to the pot"? Surface and reality
The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
Do it yourself smart home: intelligent air conditioning control
[untitled]
力扣5: 最长回文子串
【保姆级】包体积优化教程
[pytorch] picture enlargement
【pytorch】微调技术
[day_030420] find the longest consecutive number string in the string
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
Upgrade appium automation framework to the latest 2.0