28. Implement strStr()
2016-02-21 16:56
393 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Solution:
运行速度:快
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Solution:
class Solution { public: int strStr(string haystack, string needle) { int sz1 = haystack.size(); int sz2 = needle.size(); if(sz1<sz2) return -1; else if(sz1==0||sz2==0) return 0; else { for(int i=0;i<sz1-sz2+1;i++){ if(haystack[i]==needle[0]){ string tmp = haystack.substr(i,sz2); if(tmp==needle) return i; } } return -1; } } };心得:问题比较简单,注意空集的情况返回0这个corner就行
运行速度:快
相关文章推荐
- 何为Java引用
- 杭电1175——连连看(DFS)
- spark_3:spark的基础
- 高精度模板总结(string 实现加、减、乘、除)常用版
- JSP内置对象
- Windows命令行视频教程-01-简介命令提示符
- 点击某一个cell中的button,确定这个button所在的行(找superview)
- g++编译时默认支持C++11的几个配置小方法
- android 网络框架性能优化分析
- CodeForces512C-Pluses everywhere-模拟/数学/排列组合模板
- c++的坚持
- Angular JS中scope的作用域
- 树莓派 安装 系统 于配置
- 自己写配置文件
- Binary Tree Right Side View
- 将一句话中单词进行倒置,标点符号不倒置
- Swift实现IOS界面的跳转
- 利用AVPlayer播放iOS沙盒中的视频
- 被嘲笑没有智商的题
- 1. redis简介