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 ...
随机推荐
- Java学习——Number类、Character类
Number类 在使用数字时,我们通常会使用内置数据类型,如 int a = 9; float b = 3.14 然而在实际开发中,我们经常遇到需要使用对象而不是使用内置数据类型的对象.为了解决这一问 ...
- Gradle Cheat Sheet
加快编译速度 使用 gradle 2.4 及以上版本 ~/.gradle/gradle.properties 加入如下配置 org.gradle.daemon=true org.gradle.jvma ...
- SQL2012新特性一次一个数据块----特殊的查询分页
对于数据库人员来说,在软件项目开发的过程中,或多或少的不可避免的就是经常遇到类似“UI查询结果设计”这样的说法,当听到UI设计的时候,多数人的第一反应是“靠!关我毛事!~这是其他人的问题!”. 然而, ...
- js数组去重的4个方法
面试前端必须准备的一个问题:怎样去掉Javascript的Array的重复项, 这个问题看起来简单,但是其实暗藏杀机. 考的不仅仅是实现这个功能,更能看出你对计算机程序执行的深入理解. 我总共总结4种 ...
- paper 105: 《Single Image Haze Removal Using Dark Channel Prior》一文中图像去雾算法的原理、实现、效果及其他
在图像去雾这个领域,几乎没有人不知道<Single Image Haze Removal Using Dark Channel Prior>这篇文章,该文是2009年CVPR最佳论文.作者 ...
- :enabled 匹配所有可用元素
描述: 查找所有可用的input元素 HTML 代码: <form> <input name="email" disabled="disabled&qu ...
- 处理SecureCRT中使用vim出现中文乱码问题
处理SecureCRT中使用vim出现中文乱码问题 引用原文:http://blog.chinaunix.net/uid-20639775-id-3475608.html因为cat没有问题,定位是vi ...
- 2015弱校联盟(1) -J. Right turn
J. Right turn Time Limit: 1000ms Memory Limit: 65536KB frog is trapped in a maze. The maze is infini ...
- spark统计
http://www.myexception.cn/sql/2004512.html http://blog.csdn.net/ssw_1990/article/details/52220466 ht ...
- c++实现螺旋矩阵分析总结
螺旋矩阵,是这么一个东西: 1 2 3 8 9 4 7 6 5 这是一个,n*n的矩阵,由外向里一次递增,一环一环,就好像一个螺旋一样.不难想象,如果n=5,那么应该是这样的: ...