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. SDUT OJ 2463 学校password你必须学会科学计划

    #include<iostream> #include<string.h> #include<stdio.h> #define N 10010 #define M ...

  2. Javascript继承之最佳实践

    尊重原创,转载请注明出处:http://blog.csdn.net/zoutongyuan 什么是继承? 继承是面向对象最显著的一个特性.继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和 ...

  3. 王立平--result += &quot;{&quot;;

    result += "{"; 等于:result=result+"{" 字符串连接 x+=1====x=x+1 版权声明:本文博客原创文章,博客,未经同意,不得 ...

  4. zoom的学习

    上大学做阶段项目时遇到了一个非常奇特的现象:kindEditor上传图片功能失效,可是把jsp所引用的样式去掉就好用,这说明样式有问题,于是删一个样式測试一下,就这样罪魁祸首落在了zoom身上,这是我 ...

  5. C#和Java中执行SQL文件脚本的代码(非常有用)

    原文:C#和Java中执行SQL文件脚本的代码(非常有用) 我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析 去 ...

  6. JS数据绑定模板artTemplate试用

    之前写JS绑定数据曾经用过tmpl库,虽然功能比较强大但是感觉不是很轻量,对于相对简单的数据需求显得有些臃肿.而Ajax返回数据自己拼接html的方式又显得不够高端,因此今天看了一篇介绍artTemp ...

  7. 利用css新属性appearance优化select下拉框

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. VAXVOIP SDK Licensekey

    Insert the following key to the SDK with SetLicenseKey call: VAXVOIP.COM-191P238P55P253P97P229P51P76 ...

  9. javascript之Style物

    Background 属性 属性                                 描写叙述 background                      在一行中设置全部的背景属性 ...

  10. C++调用一个成员函数的需求this指针的情况

    1.虚成员函数,因为需要this展望虚表指针的指针 2.在数据成员的操作部件的功能 #include "stdafx.h" #include <iostream> #i ...