[LeetCode]Implement strStr()
2015-11-03 16:27
309 查看
题目描述:(链接)
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解题思路:
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) { int sub_len = needle.size(); int total_len = haystack.size(); if (sub_len > total_len) { return -1; } if (haystack == needle) { return 0; } int result = -1; for (int i = 0; i <= total_len - sub_len; ++i) { if (haystack.substr(i, sub_len) == needle) { result = i; break; } } return result; } };
相关文章推荐
- WPF 异步加载高清大图
- 博科300光纤交换机配置手册/操作方法/密码设置/用户指南大全
- service详解
- Gone with the wind on women's enlightenment
- ESP8266编译环境 对于习惯Ubuntu默认桌面的用户非常好
- CI自定义js,css的url
- 利用percona-xtrabackup快速搭建MySQL数据库主从复制
- 利用percona-xtrabackup快速搭建MySQL数据库主从复制
- java中的匿名内部类总结
- YII2设置路由访问,美化url
- java mail
- Eclipse署动态web项目方法
- oracle 存储过程编辑 卡死
- 安卓学习教程总结:芯灵思开发板安卓底层开发
- request,response,session,cookie,application
- 使用C++复制文件
- App 开发:Hybrid 架构下的 HTML5 应用加速方案
- 迁移OCR、表决磁盘和asm spfile到其他磁盘组中
- matlab练习程序(奇异值分解压缩图像)
- java list 根据对象一个字段去重