[leetcode] 28.Implement strStr()
2015-08-31 18:50
573 查看
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题意:
实现strstr(),找到子串在原串中出现的第一个下标。
代码如下:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题意:
实现strstr(),找到子串在原串中出现的第一个下标。
代码如下:
class Solution { public: int strStr(string haystack, string needle) { if (needle.empty())return 0; else if(haystack.empty())return -1; int needleSize = needle.length(); int haystackSize = haystack.length(); for (int i = 0; i <= haystackSize - needleSize; i++) { int j = 0; for (; j < needleSize; j++) { if (haystack[i + j] != needle[j])break; } if (j == needleSize)return i; } return -1; } };
相关文章推荐
- Ubuntu修改默认键盘布局的方法(适用于13.04|14.04)
- 植物大战僵尸2天空之城安卓版发布
- 手机卡未实名真会停机 实名登记办理教程
- 软件测试(第一章)
- 天声人語 20150831
- Java下载网络上的图片
- C++ 映射共享文件到本地
- apache fcgi机制核心实现代码
- 2015年云计算的三大发展趋势
- java相关问题回答
- How to Tell Science Stories with Maps
- WiFi与WLAN的区别
- win10应用安装位置修改方法
- [底层] 为什么Integer.MIN_VALUE-1会等于Integer.MAX_VALUE
- 测试基础 同行评审
- Apache和Tomcat
- testlink
- 映像备份与恢复管理工具Easy Image X使用说明
- 正则表达式 grep egrep
- 整合 spring 4(包括mvc、context、orm) + mybatis 3 示例