Implement strStr().

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

额,当然我用的就是暴力搜索来,没用到KMP之类的算法,有时间再来补上辣,代码如下:

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

LeetCode OJ:Implement strStr()(实现子字符串查找)的更多相关文章

  1. Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution

    class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...

  2. 数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找

    数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找 Boyer-Moore字符串查找算法 注意,<算法4>上将这个版本的实现称为Broyer-Moore算法,我看了 ...

  3. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. [leetcode] 21. Implement strStr()

    这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了. 先放题目吧: Implement strStr(). Re ...

  6. [LeetCode] 28. Implement strStr() ☆

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

  8. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  9. leetcode(57)- Implement strStr()

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

随机推荐

  1. Spark --【宽依赖和窄依赖】

    前言 Spark中RDD的高效与DAG图有着莫大的关系,在DAG调度中需要对计算过程划分stage,暴力的理解就是stage的划分是按照有没有涉及到shuffle来划分的,没涉及的shuffle的都划 ...

  2. Spring AOP 的实现方式(以日志管理为例)

    一.AOP的概念 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充,流行的AOP框架有Sping AOP.Aspect ...

  3. C++编程中设置文件长度的方法

    转自:http://blog.csdn.net/rrrfff/article/details/6705685 bool SetFileLength(const char *FilePath, off_ ...

  4. php 中处理 websocket

    http://www.cnblogs.com/hustskyking/p/websocket-with-php.html 下面我画了一个图演示 client 和 server 之间建立 websock ...

  5. jQuery :gt 选择器 jQuery :lt 选择器

    选择前 3 个之后的所有 <tr> 元素: $("tr:gt(2)"); 选择前 2 个 <tr> 元素: $("tr:lt(2)");

  6. nginx简介及优点总结

    简介:nginx是web服务器,由C语言开发,基于事件驱动能处理百万级别的tcp连接,高度模块化的设计和自由的许可证使得扩展其功能的模块层出不穷, 跨平台,可使用当前操作系统特有的一些高效API来提高 ...

  7. c# c++通信--命名管道通信

    进程间通信有很多种,windows上面比较简单的有管道通信(匿名管道及命名管道) 最近做个本机c#界面与c++服务进行通信的一个需求.简单用命名管道通信.msdn都直接有demo,详见下方参考. c+ ...

  8. docker使用Mesos

    https://github.com/PyreneGitHub/mesos_use/tree/master

  9. 拓扑排序(dfs)

    int c[N];//c[u]=0表示从来没有访问过:c[u]=1表示已经访问过,并且还递归访问过它的所有子:c[u]=-1表示正在访问. int topo[N],t; int G[N][N]; bo ...

  10. ElasticSearch集群故障案例分析: 警惕通配符查询

    最近ElasticSearch集群出现了 https://elasticsearch.cn/article/171 文章中描述的情况,现在转载全文警示下自己. 许多有RDBMS/SQL背景的开发者,在 ...