28.实现 strStr() 函数

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

代码:

class Solution {
public:
int strStr(string haystack, string needle) {
if(needle .empty())
return 0;
for(int i= 0;i<haystack.length();i++)
{
if(haystack[i] == needle[0])
{
int n =i;
for(int j = 0;j<needle.size();j++)
{
if(haystack[n++] != needle[j])
break;
else
if(n - i == needle.size())
return i;
}
}
}
return -1;
}
};

笔记:超时了。。

28.实现 strStr() 函数的更多相关文章

  1. C语言中的strstr函数

    转自:http://www.cnblogs.com/xy-kidult/archive/2012/12/25/2832460.html 早上翻<C和指针>,碰见一个子串查找问题,这个问题在 ...

  2. <每日 1 OJ> -LeetCode 28. 实现 strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  3. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  4. 【LeetCode】28. 实现 strStr()

    28. 实现 strStr() 知识点:字符串:KMP算法 题目描述 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 ne ...

  5. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  6. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  7. C语言中strstr函数

    头文件:#include <string.h> strstr()函数用来检索子串在字符串中首次出现的位置,其原型为:    char *strstr( char *str, char * ...

  8. strstr 函数用法

    strstr 编辑 strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. C语言函数 编辑 ...

  9. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

随机推荐

  1. Opencv-Python学习笔记(二)

    2. 使用OpenCV3处理图像 2.1 不同色彩空间的转换 OpenCV中有数百种关于在不同色彩空间之间转换的方法. 三种常用色彩空间:灰度.BGR.HSV(Hue色调,Saturation饱和度, ...

  2. Distance on the tree

    Distance on the tree https://nanti.jisuanke.com/t/38229 DSM(Data Structure Master) once learned abou ...

  3. pm2管理node

    一般直接npm start起的退出命令行就没了,node后台管理工具pm2目前比较流行. npm install -g pm2 pm2 list pm2 start bin/www --name de ...

  4. 记忆化搜索 P1464 Function

    题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,20,20) 如果a< ...

  5. 摘选改善Python程序的91个建议2

       62.metaclass stackflow          中文翻译    63.Python对象协议   https://zhuanlan.zhihu.com/p/26760180     ...

  6. rest_framework的视图组件继承过哪些类?

  7. Centos7 出现Welcome to emergency mode!

    做mount挂载时,修改了  /etc/fstab 文件,导致Centos7重启时出现如下图所示错误: Welcome to emergency mode! After logging in, typ ...

  8. select 两层 第二个select需要加别名

    select t.id from (select xxx) t

  9. Scania SDP3 2.38.2.37.0 Download, Install, Activate: Confirmed

    Download: Scania Diagnos & Programmer SDP3 2.38.2.37.0 free version and tested version SDP3 2.38 ...

  10. hive资料

    Hive基本操作 Hive 解锁操作 之前使用Hive,出现过一种情况:在代码正在执行insert into或insert overwrite时,中途手动将程序停掉,会出现卡死情况,只能执行查询操作, ...