Implement strStr() [LeetCode]
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]的更多相关文章
- Implement strStr() leetcode java
题目: Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode(28)题解:Implement strStr()
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- RDIFramework.NET ━ 9.8 用户权限管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.8 用户权限管理 -Web部分 在实际应用中我们会发现,权限控制会经常变动,如:需要调整角色的分配,需要收回与授予某些角色.用 ...
- EBS R12.2 创建应用层的启动和关闭脚本
Create the following files to start and stop R12. application tier. Change the apps and weblogic pas ...
- 【转修正】sql server行版本控制的隔离级别
在SQL Server标准的已提交读(READ COMMITTED)隔离级别下,一个读操作会和一个写操作相互阻塞.未提交读(READ UNCOMMITTED)虽然不会有这种阻塞,但是读操作可能会读到脏 ...
- Mac下安装UPnP Inspector
由于工作中需要用到UPnP Inspector这个工具,而这个工具在windows下安装非常简单,在Mac下安装却很麻烦,在此记录安装流程. 这个工具依赖于两个其他的库:Coherence(一个DLN ...
- sdutoj 2623 The number of steps
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2623 The number of steps ...
- html5中的beginPath与stroke
名词解释: 定义和用法 beginPath() 方法在一个画布中开始子路径的一个新的集合. 语法 beginPath() 描述 beginPath() 丢弃任何当前定义的路径并且开始一条新的路径.它把 ...
- struts 初体验
1. 什么是Struts2 struts2是以WebWork的设计思想为核心,吸收了Struts1的部分有点,建立了兼容WebWork和Struts1的MVC框架. 1.1 WebWork: 强调系统 ...
- 《zw版·delphi与Halcon系列原创教程》THOperatorSetX版hello,zw
<zw版·delphi与Halcon系列原创教程>THOperatorSetX版hello,zw 下面介绍v3版的hello,zw. Halcon两大核心控件,THImagex.THOpe ...
- debuggee python
my_debugger_defines.py #encoding:utf-8 from ctypes import * from sys import version as py_ver # In p ...
- each(callback) 对于每个匹配的元素所要执行的函数
以每一个匹配的元素作为上下文来执行一个函数. 意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素).而且,在每次执行函数时,都会给函数传递一 ...