Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

就是判断haystack中是否有needle,如果包含的话,返回第一次包含的地址。如果没有则返回NULL。

这题官网打出来的是easy,但是要做好绝不简单。我能想到的就是O(m*n)的复杂度了。最经典的是用KMP算法。

class Solution {

public:
char *strStr(char *haystack, char *needle)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
int lena = strlen(haystack);
int lenb = strlen(needle);
if(lena < lenb)
return NULL;
if(lena == lenb)
{
if(strcmp(haystack, needle)==)
return haystack;
return NULL;
}
for(int i=; i<=lena-lenb; i++)
{
bool flag = true;
for(int j=; j<lenb; j++)
{
if(haystack[i+j] != needle[j])
{
flag = false;
break;
}
}
if(flag)
return haystack + i;
}
return NULL;
}
};

leetcode第27题--Implement strStr()的更多相关文章

  1. 【JavaScript】【KMP】Leetcode每日一题-实现strStr()

    [JavaScript]Leetcode每日一题-实现strStr() [题目描述] 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字 ...

  2. Leetcode 详解(Implement strstr)

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

  3. LeetCode记录之28——Implement strStr()

    Implement strStr(). Returns 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 nee ...

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

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

  6. 【leetcode❤python】 28. Implement strStr()

    #-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(se ...

  7. leetcode第27题:移除指定元素

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  8. LeetCode 第27题--移除元素

    1. 题目 2.题目分析与思路 3.代码 1. 题目 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2. 你不需要考虑数组 ...

  9. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. C++ - 派生类访问模板基类(templatized base class)命名

    派生类访问模板基类(templatized base class)命名 本文地址: http://blog.csdn.net/caroline_wendy/article/details/239936 ...

  2. Java它配备了一个很好的工具2

    Jconsole 本机java自带的系统monitor具,它也可以连接到的本地远程连接java process,联系java process申请后可查看CPU,内存,主题.GC事件,能帮忙看看系统是否 ...

  3. Play Framework + ReactiveMongo

    Play Framework + ReactiveMongo Play!是一个full-stack(全栈的)Java/Scala Web应用框架,包括一个简单的无状态MVC模型,具有Hibernate ...

  4. BZOJ 2115 Wc2011 Xor DFS+高斯消元

    标题效果:鉴于无向图.右侧的每个边缘,求一个1至n路径,右上路径值XOR和最大 首先,一个XOR并能为一个路径1至n简单的路径和一些简单的XOR和环 我们开始DFS获得随机的1至n简单的路径和绘图环所 ...

  5. [程序安装包制作] Advanced Installer 备忘

    原文:[程序安装包制作] Advanced Installer 备忘 Product Information - Product Details 这个重点是Product Version.讲这个之前, ...

  6. webserver实现

    最近的工作需求client和server使用https协议进行通讯,我负责client编写程序,在操作系统的-depth理解认为一旦前webserver实现,整理代码: #include"a ...

  7. ActionBarActivity: cannot be resolved to a type

    "the import android.support.v7 cannot be resolved  " or "ActionBarActivity: cannot be ...

  8. .NET缓存框架CacheManager---1、CacheManager的介绍

    在我们开发的很多分布式项目里面(如基于WCF服务.Web API服务方式),由于数据提供涉及到数据库的相关操作,如果客户端的并发数量超过一定的数量,那么数据库的请求处理则以爆发式增长,如果数据库服务器 ...

  9. int 价值型追记-time

    一个 30 M 的int号码值一次性保存.和读取一次,避免保存为char 种类,进行格转变,格转换时间是非常耗时. #include <iostream> #include <fst ...

  10. 详解JMeter函数和变量

    JMeter函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用.函数调用的语法如下: ${__functionName(var1,var2,var3)} 其中,__function ...