28. Implement strStr()
来源:互联网 发布:云软件多少钱呢 编辑:程序博客网 时间:2024/06/07 01:56
28. Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题目比较简单,就是从字符串haystack中找出是否包含字符串needle,如果包含则返回匹配点的索引值,如果不包含则返回-1。
不采用KMP算法,在字符串haystack和needle的首位开始循环,当在haystack中匹配到needle第一位时开始逐位判断,若想等返回此时haystack字符串的索引值,若不相等则再从haystack中寻找下一个匹配点,直到到达两字符串size值之差的索引处停止,若没有发现匹配则返回-1。
class Solution {public:int strStr(string haystack, string needle) {int h = haystack.size();int n = needle.size();if (n == 0) return 0;if (h == n&&haystack == needle) return 0;for (int i = 0; i <= h - n; i++) {if (haystack[i] == needle[0]) {bool f = true;int j = 1;while (j < n) {if (haystack[i + j] != needle[j]) {f = false;break;}j++;}if (f) return i;}}return -1;}};编程小白,效率不算高,但是思路十分通俗易懂,写起来也很快,看答案时了解到KMP算法,下篇尝试用KMP算法来实现此题。
0 0
- [LeetCode]28.Implement strStr()
- LeetCode --- 28. Implement strStr()
- LeetCode 28.Implement strStr()
- [Leetcode] 28. Implement strStr()
- 28. Implement strStr()
- 【leetcode】28. Implement strStr()
- [leetcode] 28.Implement strStr()
- Leetcode-28.Implement strStr()
- 28. Implement strStr()
- 28. Implement strStr()
- 28. Implement strStr()
- 28. Implement strStr() LeetCode
- 28. Implement strStr()
- leetcode 28. Implement strStr()
- [LeetCode]28. Implement strStr()
- 28. Implement strStr()
- 28. Implement strStr()
- 28. Implement strStr()
- Mysql 事务ACID特性及事务隔离级别
- 关于 “如何传递一个大小不定的二维数组给函数” 的一个小想法
- 决策树笔记
- javaweb之jsp内置对象
- Git常用操作指南(附图文)
- 28. Implement strStr()
- Struts配置文件struts.xml详解
- 跨域问题
- vb.net 教程 5-10 Graphics类 1
- mongo connections url string 的问题
- 不使用框架来发送数据到服务器后台
- HDU2031 进制转换
- c++ stack 栈
- amd模块定义模式