leetcode-28.实现strStr()
题意
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
样例
示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:
当
needle是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。对于本题而言,当needle是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
算法
如果haystack字符串为空,直接返回0;
若needle长度大于haystack,返回-1;
遍历给定haystack字符串,如果存在元素与needle字符串的首字符相同,继续;否则,返回 -1;
比较后续字符串,若都相同,返回当前索引值。(可以使用string.substr(index1, len))来实现
code
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle == "")
return ;
int ans = -;
int i, j;
bool flag;
for(i=; i<haystack.length(); i++)
{
if(haystack[i] == needle[])
{
flag = true;
for(j=; j<needle.length(); j++)
{
if(haystack[i+j] != needle[j])
{
flag = false;
break;
}
}
if(flag == true)
{
ans = i;
break;
}
}
}
return ans;
}
};
后来更新的
leetcode-28.实现strStr()的更多相关文章
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- <每日 1 OJ> -LeetCode 28. 实现 strStr()
题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [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(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- Docker面试题
1.如何列出可运行的容器?docker ps 2.启动nginx容器(随机端口映射),并挂载本地文件目录到容器html的命令是?docker run -d -P --name nginx2 -v /h ...
- logstash常用插件解析
官方地址:https://www.elastic.co/guide/en/logstash-versioned-plugins/current/index.html 配置文件写法: # 日志导入inp ...
- 正则表达式的一些探索(偏JavaScript)
简单的探索下正则表达式的相关知识,首先先了解下正则表达式的引擎和匹配过程区别,再试着掌握如何在场景中编写正则表达式,再然后探索下根据上文已知的原理和编写过程怎么去优化正则表达式,最后给出一些js里正则 ...
- 使用 AcceptTcpClientAsync 进行 异步 操作
转自:https://gist.github.com/jamesmanning/2622054 using System; using System.Collections.Generic; usin ...
- linux 命令 — 文件相关
使用文件相关命令 dd 用来生成任意大小的文件 dd if=/dev/zero of=junk.data bs=1m count=1 生成一个1m大小的文件,里面全部使用0填充 if: 指定输入文件, ...
- OnlineJudge难度与正确度的相关性检验
本着做题的心态,上了东莞理工学院的 oj 网:在选择难度的时候发现有些题目通过率和难度可能存在着某些关系,于是决定爬下这些数据简单查看一下是否存在关系. 一.新建项目 我是用 Scrapy 框架爬取的 ...
- easyui datagrid列显示图片
表格头 显示图片 jquery
- 谈谈 JAVA 的对象序列化
所谓的『JAVA 对象序列化』就是指,将一个 JAVA 对象所描述的所有内容以文件 IO 的方式写入二进制文件的一个过程.关于序列化,主要涉及两个流,ObjectInputStream 和 Objec ...
- Jenkins结合.net平台工具之Nunit
有时候我们需要对从git上拉取的项目进行单元测通过以后才可以发布到测试环境,.net平台下单元测试的框架也很多例如mstest,nunit,xunit等,下面以Nunit为例讲解如何通过Jenkins ...
- Centos-7修改yum源为国内的yum源
以centos7为例 ,以 修改为阿里的yum源 1. 备份本地yum源 [root@localhost yum.repos.d]# cp CentOS-Base.repo CentOS-Base.r ...