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. tomcat源码调试2

    前面对tomcat做了一些简单的认识,下面将tomcat源码调试环境搭建起来. 可以参考官网的搭建方法,这里是按照网上的maven管理的方式搭建. 大概步骤是: 1.下载tomcat 9的源码,一般是 ...

  2. linux中uptime命令获取主机运行时间和查询系统负载信息

    系统中的uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行了多长时间.目前有多少登陆用户.系 ...

  3. [转]手把手教你搭建Hive Web环境

    了解Hive的都知道Hive有三种使用方式——CLI命令行,HWI(hie web interface)浏览器 以及 Thrift客户端连接方式. 为了体验下HWI模式,特意查询了多方的资料,都没有一 ...

  4. 机器学习与R语言:C5.0

    #---------------------------------------- # 功能描述:演示C50建模过程 # 数据集:汉堡大学信贷模型,信贷数据 # #------------------ ...

  5. linux内核动态打印

    参考:https://www.cnblogs.com/pengdonglin137/p/4622460.html https://linux.cn/article-3682-1.html?pr 如何打 ...

  6. Swoole学习(七)Swoole之异步TCP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...

  7. 2045331 《Java程序设计》第09周学习总结

    2045331 <Java程序设计>第09周学习总结 教材学习内容总结 第十六章 整合数据库 16.1.1JDBC简介 1.JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接 ...

  8. vROPS中获取虚拟机在VC中的UUID

    vROPS中虚拟机对象的ID为resourceID,跟vCenter中虚拟机的UUID是不一致的,因此想要将vROPS中的虚拟机和vCenter中的虚拟机对应肯定不能靠虚拟机名称,而是一定要靠UUID ...

  9. Centos7 Python3.x源码安装

    第一步,安装开发工具集 yum -y groupinstall "Development tools" 第二步,安装相关依赖包: yum -y install zlib-devel ...

  10. [BZOJ4756]Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...