Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button  to reset your code definition.

Hide Tags

Two Pointers String

class Solution {
public:
int strStr(char *haystack, char *needle) {
int hayl=strlen(haystack);
int nel=strlen(needle);
for(int i=;i<=hayl-nel;++i){
char *h=haystack+i;
char *n=needle;
while(*n!='\0'){
if(*n!=*h)
break;
else{
++h;
++n;
}
}
if(*n=='\0')
return i;
}
return -;
}
};

Implement strStr() 字符串匹配的更多相关文章

  1. php -- strstr()字符串匹配函数(备忘)

    Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() ...

  2. leetcode——Implement strStr() 实现字符串匹配函数(AC)

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  3. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

  4. 字符串匹配:KMP

    參考:从头到尾彻底理解KMP 在字符串 str 中 匹配模式串 pattern 1. 计算模式串的 next 数组: 2. 在字符串中匹配模式串:当一个字符匹配时,str[i++], pattern[ ...

  5. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  6. LeetCode 28:实现strStr() Implement strStr()

    爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needl ...

  7. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. 70. Implement strStr() 与 KMP算法

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

随机推荐

  1. mongodb本地搭建过程

    1.解压安装包后安装 安装时注意:1.选择customs    2.路径选择C盘以外的盘符 安装完成后: 2.在bin的同级目录下新建data.log文件夹 3.在data文件夹下新建db文件夹,在l ...

  2. [转]js设计模式—发布订阅模式

    发布—订阅模式又叫观察者模式,它定义对象间的一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知.在javascript开发中,一般用事件模型来替代传统的发布—订阅模式.本文将 ...

  3. spring boot指定外部配置的坑

    外部配置文件所在目录path/to/dir 指定--spring.config.location=path/to/dir 项目启动,没有使用任何配置文件,项目外和jar包中的都没有使用 这是因为其把p ...

  4. No module named zope.interface error 的解决

    在 import zope.interface 时,出现错误 No module named zope.interface error根据 http://stackoverflow.com/quest ...

  5. Intent传递list集合时异常解决

    以前只是用intent传递一些简单的值,最近传递list集合时发现值总是传不过去,logcat报如下错误 说的是不能处理值为null的情况,回过头看list集合时确实发现有value为null的key ...

  6. oracle习题-emp表查询练习

    emp表查询练习 1 查询emp表的全部记录 Select * from emp; 2 查询出每个雇员的编号.姓名.基本工资 Select empno,ename,sal from emp; 3 查询 ...

  7. Codeforces 1150D(字符串dp)

    反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...

  8. ML面试1000题系列(61-70)

    本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 61.说说共轭梯度法? @wtq1993,htt ...

  9. Postgresql 正则表达式【转】

    原文:http://blog.csdn.net/wugewuge/article/details/7704996 postgresql支持POSIX 风格的正则表达式,在postgresql中使用正则 ...

  10. C++之自定义key类型,重载操作符

    #include <map>#include <string>using namespace std;class MyString{ public:MyString(){m_s ...