Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

Summary: be careful about the corner case, haystack = "", needle = ""

     char *strStr(char *haystack, char *needle) {
if(haystack[] == '\0' && needle[] == '\0')
return haystack; int hay_idx = ;
while(haystack[hay_idx] != '\0'){
bool find = true;
int tmp_idx = hay_idx;
int needle_idx = ;
while(needle[needle_idx] != '\0'){
if(haystack[tmp_idx] == '\0')
return NULL;
if(haystack[tmp_idx] != needle[needle_idx]){
find = false;
break;
}
tmp_idx ++;
needle_idx ++;
}
if(find)
return haystack + hay_idx;
else
hay_idx ++; }
return NULL;
}

Implement strStr() [LeetCode]的更多相关文章

  1. Implement strStr() leetcode java

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

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

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

  3. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  4. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  5. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  6. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...

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

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

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

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

随机推荐

  1. HTTP下载文件名称编码说明

    HTTP下载保存文件名 下载文件需要保存的名称 在响应报文头中 Content-Disposition 响应报文头域中指定, 例如 Content-Disposition: attachment; f ...

  2. apache php env build

    from http://blog.csdn.net/yincg/article/details/8782364 1.1 系统说明Centos 6.2 (最小化安装)1.2 软件说明httpd-2.4. ...

  3. Leetcode: Arithmetic Slices II - Subsequence

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  4. overflow:hidden---清除浮动,隐藏溢出

    overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解.一提到清除浮动,我们就会想到另外一个CSS样式: ...

  5. (一)s3c2440 地址分配讲解 (很难很纠结)

    mini2440的地址怎么分配.mini2440处理器的地址怎么分配. S3C2440处理器可以使用的物理地址空间可以达到4GB,其中前1GB的地址(也就是0x0000 0000--0x4000 00 ...

  6. Java语言的编写规范

    ,区别大小写.2,注意编程格式,比如对齐啊 什么的.3,注释一定要写好,有统一的// 或者/* */,这样让程序好看.4,定义变量的时候竟让别人看明白,不要A 啊B 啊什么的 要用英语单词这类的,简而 ...

  7. Java 利用初学知识 写出自己的名字

  8. Bellman-Ford

    看来一千个acmer有一千个迪杰斯特拉,Bellman-Ford也是一样. 看了刘汝佳的bellman-ford,简直和spfa一模一样啊!!! 松弛n -1 次还是可以松弛,说明有负环; 刘汝佳写得 ...

  9. How to generate a random number in R

    Generate a random number between 5.0 and 7.5x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数x2 <- runif ...

  10. centos minimal 开启无线网卡 & 查看IP

    minimal版本默认不启动网络,所以要自己配置. 配置过程很简单,编辑配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 需要更改两项 NM_CONTR ...