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 文中代码为了不动官网提供的初始 ...
随机推荐
- 推荐2一个在Java编码过程中得心应手的工具
推荐2在编码过程中的减小不仅编码的量,挺easy工具上手:可适用Java反思与单探头Assert. 1 Mirror:Java反思 简单介绍 官网:http://projetos.vidageek.n ...
- cocos2dx tolua传递参数分析
cocos2dx tolua传递参数分析: tolua_Cocos2d_CCNode_addChild00 == void CCNode::addChild(CCNode *child) tolua_ ...
- hosts目录位置
C:\WINDOWS\system32\drivers\etc 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- 怎么样eclipse发达国家多重聚合关系maven项目和使用git管理
最近使用的项目的开发maven,多于maven有项目之间有一定的联系,因此,创建一个单独的,然后,maven聚合管理. 项目采用git要管理代码.由于上传的代码集时,.gitignore不要上传文件. ...
- How to install IIS 7.5 on Windows 7 using the Command Line
原文 How to install IIS 7.5 on Windows 7 using the Command Line On Windows Vista, to install IIS 7.0 f ...
- Cisco笔试——2014年
笔试时间:11月1日(周六)下午13:00-14:10 申请者考场地点:上海市徐汇区梅陇路130号华东理工大学徐汇校区第六教学楼六大 我报的思科的软件开发software,属于(常规) 一共70多道题 ...
- Helloworld with c
CentOS 7 之Helloworld with c 其实我也不知道是为了啥, 到了现在这种年纪还想学习Linux下的C语言编程.因为我一直就傻傻地认为机会是垂青有准备的人,也一直呆呆地认为活到 ...
- mysql只导出表结构或数据
唯一的非导电结构指南数据 mysqldump -t 数据库名称 -uroot -p > xxx.sql 指南结构不仅指导数据 mysqldump --opt -d 数据库名 -u -p ...
- 谈论Hibernate级联删除——JPA根据Hibernate实现许多级联删除CascadeType.DELETE_ORPHAN
声明: 1.这篇文章是原创.非复制或转载过来. 2.在本文中,参数都亲自做过实验证明. 3.这篇文章谈到了Hibernate配置基于注释的方法.hbm语法不可用. 不清JPA.Hibernate.EJ ...
- js敏感词过滤
var filterWord={ words:"", tblRoot:{}, //敏感词文件 file:"sensitiveWords.txt", //载入敏感 ...