Implement strStr
2015-10-16 21:27
267 查看
mplement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
先来暴力解法: (kmp看懂后再补。。)
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
先来暴力解法: (kmp看懂后再补。。)
class Solution { public: int strStr(string haystack, string needle) { if (needle == "") return 0; int i = 0; int j = 0; while (i < haystack.size() && j < needle.size()) { if (haystack[i] == needle[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if (j == needle.size()) return i - j; else return -1; } };
相关文章推荐
- 什么是计算金融
- 命运(经典dp)
- Python--装饰器
- 解决GP服务产生的结果无法自动发布为地图服务的问题
- 【特征匹配】PCA-SIFT原理及源码解析
- 工作中svn 使用记录
- 各种Adapter的用法
- 解决 “invalid package Applications built with SDK 9.0 or later must be packaged as proper IPA files.”s
- 用两个栈实现队列
- HTTP
- 学习Unity 3d,跟着雨潭老师的脚步前进。。。
- 解决GP服务产生的结果无法自动发布为地图服务的问题
- ADO学习笔记之注入漏洞与参数化查询
- c语言 源代码到可执行程序的过程
- 以Android环境为例的多线程学习笔记———-Callable与Future
- softmaxCost实现
- 【LeetCode】Ugly Number 解题报告
- 第二章、供应源搜寻战略
- android学习
- 【面试题】C语言:使用函数实现两个数的交换