leetcode第27题--Implement strStr()
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()的更多相关文章
- 【JavaScript】【KMP】Leetcode每日一题-实现strStr()
[JavaScript]Leetcode每日一题-实现strStr() [题目描述] 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字 ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode记录之28——Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode(28)Implement strStr()
题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...
- 【算法】LeetCode算法题-Implement strStr
这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...
- 【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object): def strStr(se ...
- leetcode第27题:移除指定元素
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- LeetCode 第27题--移除元素
1. 题目 2.题目分析与思路 3.代码 1. 题目 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2. 你不需要考虑数组 ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- .NET单元测试艺术(2) - 第一个单元测试
List 2.1 使用[SetUp]和[TearDown]特性 using System; using System.Collections.Generic; using System.Linq; u ...
- 64位sql server 如何使用链接服务器连接Access
原文:64位sql server 如何使用链接服务器连接Access 测试环境 操作系统版本:Windows Server 2008 r2 64位 数据库版本:Sql Server 2005 64位 ...
- 爬虫(heritrix框架)
Heritrix 下载 目前 Heritrix 的最新版本是 1.14.4(2010-5-10 发布),您可以从 SourceForge(http://sourceforge.net/projects ...
- CIC and Fir 滤波器的级联
在FDATool中 CIC 和 Fir 级联滤波器的设计 1 设计CIC滤波器的幅频特性曲线如下 2.设计FIR 滤波器的幅频特性曲线如下 3.总的特性曲线如下 4.把通带部分放大后的图,比较平坦
- The maximum string content length quota (8192) has been exceeded while reading XML data
原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们W ...
- log4j+logback+slf4j+commons-logging的关系与调试(转)
背景 由于现在开源框架日益丰富,好多开源框架使用的日志组件不尽相同.存在着在一个项目中,不同的版本,不同的框架共存.导致日志输出异常混乱.虽然也不至于对系统造成致命伤害,但是明显可以看出,架构 ...
- 如何有效地记录 Java SQL 日志(转)
在常规项目的开发中可能最容易出问题的地方就在于对数据库的处理了,在大部分的环境下,我们对数据库的操作都是使用流行的框架,比如 Hibernate . MyBatis 等.由于各种原因,我们有时会想知道 ...
- scala 101
* scala 安装: 下载可以执行的文件. 注意版本. spark 0.8.0 对应的scala 为2.9.3 * scala 编译: 和java 很像: 1, 直接编译脚本: scalac H ...
- javascript/jquery读取和修改HTTP headers
javascript/jquery读取和修改HTTP headers jquery修改HTTP headers jQuery Ajax可以通过headers或beforeSend修改request的H ...
- Linux-常用命令1---对文件进行查看、复制、移动和分割
基于Linux的操作系统是一种自由和开放源代码的类UNIX操作系统. Linux的几大特点决定了它的不可代替和无法超越性: (1)免费的/开源的:(2)支持多线程/多用户: (3)安全性好; (4)对 ...