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. CentOS系统将UTC时间修改为CST时间

    1.编辑时间配置文件 # vi /etc/sysconfig/clock ZONE="Asia/Shanghai" UTC=false #设置为false,硬件时钟不于utc时间一 ...

  2. SQL Server错误与事务处理

    T-SQL中出现的错误,依据和事务的关系,可以分为两种情况: 有的错误会导致发生错误位置之后的代码不再执行,如果错误位置在事务中,该事务也会自动回滚(即在错误位置之后的rollback语句不会执行,但 ...

  3. 2016/12summary

    应用服务器处理业务逻辑,web服务器处理html文件.web服务器更加简单.应用服务器有tomcat,jboss,weblogic,web服务器有IIS,Apache. 徐总:core里面做业务逻辑, ...

  4. 刨根问底U3D---Vector3 你到底是蔬菜呢还是水果呢?

    事情的起因还是因为一段代码,因为在做一个2D TileBase的游戏 所以需要有一个简单的 Tile坐标到世界坐标的变换 public static Vector3 GetTileWorldPosBy ...

  5. Thinking in UML-2-建模基础

    建模的问题可以分为两个: 怎么建 模是什么 怎么建:角度不同决定了建模方向不同.所以首先要决定抽象的角度即建立这个模型的目的是什么. 模是什么:人+事+物+规则 我们这样来建立模型: 问题领域 = n ...

  6. logback文章推荐

    logback 配置详解:http://www.cnblogs.com/zhuawang/p/4002975.html Logback 日志输出到 mysql:http://www.codeweblo ...

  7. :only-child

    如果某个元素是父元素中唯一的子元素,那将会被匹配 如果父元素中含有其他元素,那将不会被匹配.(注:这里的其他元素并不包含文本节点,如:<p><img/>图片</p> ...

  8. Maven 使用介绍

    1,创建Project 先去官方网站下载一个最新版本http://maven.apache.org/download.cgi. 下载后解压,使用之前最好先将maven的bin目录设置到path环境变量 ...

  9. Java 菜鸟学习之 script脚本语句

    脚本语句  一般分为  顺序语句   分支语句   循环语句 顺序语句  就是从上到下依次读取 分支语句 是if语句 if(判断条件) { 满足条件执行的语句 } else { 不满足条件执行的语句 ...

  10. Android--菜单详解

    Android中的菜单分为三种,即选项菜单(系统菜单),上下文菜单和弹出式菜单. 选项菜单: 一个activity只有一个选项菜单,选项菜单的创建方式有低版本创建和高版本创建两种.最常用的是干版本创建 ...