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. java容器的数据结构-ArrayList,LinkList,HashMap

    ArrayList: 初始容量为10,底层实现是一个数组,Object[] elementData 自动扩容机制,当添加一个元素时,数组长度超过了elementData.leng,则会按照1.5倍进行 ...

  2. 关于iOS UIWebView 加载网页,点击网页内某些控件导致 Application 'UIKitApplication:xxx.xxx.xxx' was killed by jetsam.

    问题:公司用的腾讯问卷系统,内嵌在我们应用或游戏的自定义UIWebView里面展示,发现在iOS 10 以下系统,点击圆形勾选框 会大概率出现闪退. 通过联调发现:报了这样一个警告Applicatio ...

  3. C/C++结构体语法总结

    转自:http://blog.csdn.net/dawn_after_dark/article/details/73555562 结构体简介 结构体属于聚合数据类型的一类,它将不同的数据类型整合在一起 ...

  4. 【WPF】修改ComboBox样式

    修改WPF默认的ComboBox控件样式 如下图所示: 修改代码如下: <UserControl.Resources> <Style TargetType="ToggleB ...

  5. OpenStack之Glance模块

    一.Glance服务 1.Glance架构 Glance架构是一个典型的CS架构,提供的也是标准的Rest接口,各个layer分层也是很清楚的. 2.Glance服务 Glance是OpenStack ...

  6. MVc分页【关于使用扩展方法实现MVc分页】

    近期对MVc自定义分页作了一下小研究下面把他记下来 下述代1,2,3里面的代码可以直接拷贝,4以后的根据情况自己选定 1.在后台任写如下的扩展方法(任一类库都可以,但是用时得引用命名空间) // 添加 ...

  7. 20145322第一次JAVA实验报告

    20145322第一周JAVA实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验名称:Java开发环境的熟悉(Linux + Eclipse) 实验日期:2016.04.08 实 ...

  8. Finder Quick Menu FAQ

    How to use Finder Quick Menur: 1. Start Finder Quick Menu.2. Open "System Preferences -> Ext ...

  9. Juniper SRX 简单命令一

    Juniper为人所熟悉的一定是从netscreen开始的,作为一线防火墙品牌,还是有很高的地位.但是以前玩netscreen,都是用的网页版去配置,而且网页版做得很不错.但是现在netscreen要 ...

  10. 从0开始 Java学习 packet用法

    packet 包的用法 参考博客:https://www.cnblogs.com/Ring1981/p/6240412.html 用法 java 源文件带有包名,往往容易出错 如:H:\code\He ...