[LeedCode OJ]#28 Implement strStr()
2015-08-24 12:41
295 查看
[b]【 声明:版权所有,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
[/b]
题目链接:https://leetcode.com/problems/implement-strstr/
题意:
给定两个串,判定needle串是否haystack串的子串,如果是,返回匹配的起始位置,如果不是,则返回-1
思路:
直接两个循环暴力解决
[/b]
题目链接:https://leetcode.com/problems/implement-strstr/
题意:
给定两个串,判定needle串是否haystack串的子串,如果是,返回匹配的起始位置,如果不是,则返回-1
思路:
直接两个循环暴力解决
class Solution { public: int strStr(string haystack, string needle) { int len1 = haystack.length(),len2 = needle.length(); int i,j; if(len2>len1) return -1; if(len1 == len2 && haystack!=needle) return -1; for(i = 0; i<=len1-len2; i++) { for(j = 0; j<len2; j++) { if(haystack[i+j]!=needle[j]) break; } if(j == len2) return i; } return -1; } };
相关文章推荐
- jquery+CSS实现的水平布局多级网页菜单效果
- Jmeter环境搭建
- tcpdump summary
- Android-Json简单解析与生成实例(一)
- MyEclipse8.5中SVN的安装方法
- Android游戏开发之数独课时----2
- [LeedCode OJ]#27 Remove Element
- AsyncHandler
- Bash shortcuts
- 线程同步之信号量
- hdu2236
- Hyper-V Tools for win7
- CCLabel添加 显示下划线的api
- WPF 自定义文本列表
- 本地调试,ie 谷歌浏览器都正常,但是将网站部署到服务器上发现ie内核css样式出现问题
- [LeedCode OJ]#26 Remove Duplicates from Sorted Array
- html+vlc 播放多视频
- 求两个整数的平均值
- C++ STL
- ZOJ 1516--Uncle Tom's Inherited Land【二分图 && 求最大匹配数 && 经典建图】