【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() 定义相符。
【解答】
- 解法一:利用split函数分割
分割完之后的返回值是一个列表 l ,如果haystack中有needle,则 l[0]的长度就是needle第一次出现的位置
eg1:haystack = 'xxxzyn'
needle = 'zyn'
l = haystack.split(needle,1) 分割一次
l = ['xxx','zyn']
len(l[0]) = 3
即needle第一次出现的位置为3
eg2:haystack = 'xxxzyn'
needle = 'not'
l = haystack.split(needle,1) 分割一次
l = ['xxxzyn']
len(l) =1
根据 if-else 判断题条件,返回-1
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not needle: return 0
l = haystack.split(needle,1)
if len(l) == 1:
return -1
else:
return len(l[0])
执行用时:40ms
- 解法二:在haystack中遍历查找长度为len(needle)的索引范围
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not needle: return
n1=len(haystack)
n2=len(needle)
for i in range(n1-n2+1):
if haystack[i:i+n2]==needle:
return i
return -1
执行用时:36ms
【leetcode算法-简单】28. 实现strStr的更多相关文章
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- 【leetcode算法-简单】1.两数之和
[题目描述] 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- 【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object): def strStr(se ...
- Leetcode题库——28.实现strStr()
@author: ZZQ @software: PyCharm @file: strStr.py @time: 2018/11/6 20:04 要求:给定一个 haystack 字符串和一个 need ...
- LeetCode记录之28——Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode算法-简单】7.整数反转
[题目描述] 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321 示例 3: 输入: 12 ...
- 【leetcode算法-简单】66. 加一
[题目描述] 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...
- 【leetcode算法-简单】58. 最后一个单词的长度
[题目描述] 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输 ...
- 【leetcode算法-简单】9. 回文数
[题目描述] 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: ...
随机推荐
- learning scala type alise
How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, ...
- learning scala regular expression patterns
package com.aura.scala.day01 import scala.util.matching.Regex object regularExpressionPatterns { def ...
- Linux操作系统常用命令合集——第三篇-系统管理操作(25个命令)
1.whoami [命令作用] 显示当前登录有效用户名称 [命令语法] whoami [选项] [常用选项] 无 [参数说明] 用户名称 [命令示例] 显示当前登录有效用户名称 # whoam ...
- 自定义starter
https://github.com/deadzq/spring-boot-starter-hello 父子项目 子项目引用父项目中的依赖和配置参数
- fopen 的mode
转自 http://blog.csdn.net/todd911/article/details/8976543 r 打开只读文件,该文件必须存在. r+具有读写属性,从文件头开始写,保留原文件中没有被 ...
- 持续api管理翻译
- 书籍内容 > 地址: https://www.safaribooksonline.com/library/view/continuous-api-management/97814920435 ...
- 读取文件名.cpp
#include <io.h> #include <iostream> #include <string> #include <windows.h> # ...
- RK3399 修改系统默认语言为简体中文
CPU:RK3288 系统:Android 7.1 修改系统默认语言为简体中文 diff --git a/build/target/product/full_base.mk b/build/targe ...
- Python网络爬虫学习手记(1)——爬虫基础
1.爬虫基本概念 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.--------百度百科 简单的说,爬 ...
- pve_ceph问题汇总
在同一个网络内,建立了两个同名的群集 Jun 24 11:56:08 cu-pve05 kyc_zabbix_ceph[2419970]: ]} Jun 24 11:56:08 cu-pve05 co ...