https://leetcode.com/problems/implement-strstr/

题目:

Implement strStr().

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

思路:

判断一个string是否另一个string的子序列并返回位置。

naive法:遍历查找,复杂度O(mn)。

advance法还有Rabin-Karp, KMP, Boyer- Moore algorithm等。

AC代码:

 class Solution {
public:
int strStr(string haystack, string needle) {\
int m=haystack.size();
int n=needle.size();
if(n== && m==)
return ;
bool flag=true;
for(int i=;i<m-n+;i++){
for(int j=;j<n;j++){
if(needle[j]!=haystack[i+j]){
flag=false;
break;
}
}
if(flag==true){
return i;
}
else
flag=true;
}
return -;
}
};

LeetCode(28)题解:Implement strStr()的更多相关文章

  1. LeetCode(28)Implement strStr()

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

  2. LeetCode 28:实现strStr() Implement strStr()

    爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needl ...

  3. 【LeetCode】028. Implement strStr()

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

  4. LeetCode OJ:Implement strStr()(实现子字符串查找)

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

  5. 【算法】LeetCode算法题-Implement strStr

    这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...

  6. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  7. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  8. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  9. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

随机推荐

  1. 九度oj 题目1073:杨辉三角形

    题目描述: 输入n值,使用递归函数,求杨辉三角形中各个位置上的值. 输入: 一个大于等于2的整型数n 输出: 题目可能有多组不同的测试数据,对于每组输入数据, 按题目的要求输出相应输入n的杨辉三角形. ...

  2. 【bzoj4519】[Cqoi2016]不同的最小割 分治+最小割

    题目描述 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分是关于s,t的割.对于带权图来说,将所有顶点处在不同 ...

  3. SPOJ QTREE4 Query on a tree IV ——动态点分治

    [题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...

  4. py 爬取页面http://m.sohu.com 并存储

             usage()               opts,args = getopt.getopt(sys.argv[1:],                        usage( ...

  5. 常州模拟赛d2t3 小X的佛光

    平日里最喜欢做的事就是蒸发学水.[题目描述]小 X 所在的城市 X 城是一个含有 N 个节点的无向图,同时,由于 X 国是一个发展中国家,为了节约城市建设的经费,X 国首相在建造 X 城时只建造 N ...

  6. docker 给容器配置ip(和主机一个网段)

    docker 给容器配置ip(和主机一个网段).详情参考:http://www.xiaomastack.com/2015/02/06/docker-static-ip/ #/bin/bash ] || ...

  7. XPath中的轴

    这个博客中有一系列的例子,不仅有child的例子:http://www.cnblogs.com/zhaozhan/archive/2009/09/10/1563723.html XPath 是一门在 ...

  8. 转自CSDN,关于状态机

    有限状态机FSM思想广泛应用于硬件控制电路设计,也是软件上常用的一种处理方法(软件上称为FMM--有限消息机).它把 复杂的控制逻辑分解成有限个稳定状态,在每个状态上判断事件,变连续处理为离散数字处理 ...

  9. 基于CI框架的管理系统

    1:ci框架是有入口文件的,前端和后台入口文件(index.php,admin.php):里面修改$application_folder = 'application/home': 2:项目基本都是在 ...

  10. (5)ASP.NET Core 中的静态文件

    1.前言 当我们创建Core项目的时候,Web根目录下会有个wwwroot文件目录,wwwroot文件目录里面默认有HTML.CSS.IMG.JavaScript等文件,而这些文件都是Core提供给客 ...