LeetCode(28): 实现strStr()
Easy!
题目描述:
实现 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() 定义相符。
解题思路:
这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1。
然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符串相等的位置即可,这样可以提高运算效率。然后对于每一个字符,我们都遍历一遍子字符串,一个一个字符的对应比较,如果对应位置有不等的,则跳出循环,如果一直都没有跳出循环,则说明子字符串出现了,则返回起始位置即可。
C++解法:
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return ;
int m = haystack.size(), n = needle.size();
if (m < n) return -;
for (int i = ; i <= m - n; ++i) {
int j = ;
for (j = ; j < n; ++j) {
if (haystack[i + j] != needle[j]) break;
}
if (j == n) return i;
}
return -;
}
};
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 ...
随机推荐
- Makefile ------ .PHONY的作用
看下面的例子 Makefile文件 .PHONY: cleanclean: rm *.o 当Makefile文件所在目录有文件名为clean的文件,命令行“.PHONY: clean”又没添加的话,执 ...
- HSRP vs VRRP
HSRP:(Hot Standby Router Protocol)-热备份路由协议 是cisco平台一种特有的技术,是cisco的私有协议. VRRP:(Virtual Router Redunda ...
- the difference __str__ and __repr__
First, let me reiterate the main points in Alex’s post: The default implementation is useless (it’s ...
- MySQL_DDL(不定时更新)
1. //1.创建数据库,并指定字符集为utf8 create database rocker_oa default character set utf8; //2.创建用户,并指定密码为‘root’ ...
- GO语言的进阶之路-goroutine(并发)
GO语言的进阶之路-goroutine(并发) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 有人把Go比作21世纪的C 语言,第一是因为 Go语言设计简单,第二,21世纪最重要的 ...
- python---ORM之SQLAlchemy(3)外键与relationship的关系
relationship是为了简化联合查询join等,创建的两个表之间的虚拟关系,这种关系与标的结构时无关的.他与外键十分相似,确实,他必须在外键的基础上才允许使用 不然会报错: sqlalchemy ...
- sudo 找不到命令 go
错误描述 环境 CentOS7.1 x64 golang 1.9.3 golang目录:/home/moonlightwatch/go/ 环境变量配置: # /etc/profile export G ...
- CSS实现文字竖排排版
CSS实现文字竖排 注意: writing-mode: vertical-rl;存在兼容性问题,在IE上正常,在谷歌的低版本上可能识别不了. 如果文字少的话考虑直接在每一个文字中间加一个<br& ...
- 为什么要用redis
服务端的程序如何去识别客户端的状态: http是没有状态的,比如说用户A访问了服务器程序,那服务器如何知道下一次访问的时候还是A呢,这里就要用到session, 这个session是服务器的sessi ...
- 动态规划之Fib数列类问题应用
一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原 ...