28. Implement strStr()【easy】

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

解法一:

 class Solution {
public:
int strStr(string haystack, string needle) {
if (haystack.empty() && needle.empty()) {
return ;
} if (haystack.empty()) {
return -;
} if (haystack.size() < needle.size()) {
return -;
} for (string::size_type i = ; i < haystack.size() - needle.size() + ; i++) {
string::size_type j = ;
for (j = ; j < needle.size(); j++) {
if (haystack[i + j] != needle[j]) {
break;
}
} if (j == needle.size()) {
return i;
}
} return -;
}
};

最新文章

  1. Vue - class与style绑定
  2. HTML5基础之textarea 和lable
  3. linux 下配置 nodejs+ionic+cordova
  4. js计算24点
  5. Elastic Search(一)
  6. 正斜杠和反斜杠-windows、web、c语言大讨论
  7. [改善Java代码]注意Class类的特殊性
  8. jQuery 全选 反选 单击行改变背景色
  9. bzoj1899
  10. 逃出克隆岛 (codevs 2059)
  11. typedef struct 是什么意思
  12. jsp 文件使用 include指令 导入 jspf 分析,及导入jspf 文件后出现乱码问题
  13. Redux-Saga学习心得
  14. 根据URL下载图片至客户端、服务器实例
  15. hdu 2044 递推
  16. MongoDB - 2
  17. .net正则匹配
  18. docker 基础之数据管理
  19. Potential Pythonic Pitfalls
  20. 算法训练 P1101

热门文章

  1. Plus One Linked List -- LeetCode
  2. 【权值分块】bzoj1503 [NOI2004]郁闷的出纳员
  3. matlab和c混合编程
  4. JavaSE目录
  5. xib中Autolayout的使用
  6. js 鼠标左键拖动滚动
  7. 【mybatis】 mybatis在mysql 更新update 操作 更新时间字段按照年月日时分秒格式 更新为当前时间
  8. JVM类加载的那些事
  9. Python 操作mongodb 简单实例
  10. [Python爬虫] 之十九:Selenium +phantomjs 利用 pyquery抓取超级TV网数据