当前位置:网站首页>Leetcode3. Implement strstr()
Leetcode3. Implement strstr()
2022-07-06 01:38:00 【East invincible is me】
Realization strStr() function .
Here are two strings haystack and needle , Please come in haystack Find in string needle The first place the string appears ( Subscript from 0 Start ). If it doesn't exist , Then return to -1 .
explain :
When needle When it's an empty string , What value should we return ? This is a good question in an interview .
For this question , When needle When it's an empty string, we should return 0 . This is related to C Linguistic strstr() as well as Java Of indexOf() The definition matches .
Example 1:
Input :haystack = "hello", needle = "ll"
Output :2
Example 2:
Input :haystack = "aaaaa", needle = "bba"
Output :-1
Tips :
1 <= haystack.length, needle.length <= 104
haystack and needle It only consists of lowercase English characters
Personal answers
Method 1 :
def strStr(str1,str2):
return str1.find(str2)
str1="hello"
str2="ll"
print(strStr(str1,str2))
This solution uses functions built into strings , Too opportunistic .
Method 2 :
class Solution:
def strStr(self,str1,str2):
i=0
while(i<len(str1)):
if((str1[i:i+len(str2)])==str2):
return i
else:
i=i+1
else:
return -1
My own way of thinking , Slice method .( It's the idea of palindrome string before , thus it can be seen , Even if the question is finished , We must also review, It's good for doing problems in the future .)
Official answer
you 're right , I use violent matching , but KMP What is it? ?
边栏推荐
- 晶振是如何起振的?
- dried food! Accelerating sparse neural network through hardware and software co design
- Unity VR resource flash surface in scene
- What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
- 01.Go语言介绍
- Basic operations of databases and tables ----- non empty constraints
- 正则表达式:示例(1)
- 【Flask】获取请求信息、重定向、错误处理
- Force buckle 9 palindromes
- Basic process and testing idea of interface automation
猜你喜欢
leetcode刷题_验证回文字符串 Ⅱ
Basic operations of databases and tables ----- default constraints
黄金价格走势k线图如何看?
Folio. Ink is a free, fast and easy-to-use image sharing tool
Force buckle 9 palindromes
[Jiudu OJ 09] two points to find student information
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
2022年PMP项目管理考试敏捷知识点(8)
Leetcode skimming questions_ Sum of squares
leetcode刷题_反转字符串中的元音字母
随机推荐
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
Paddle framework: paddlenlp overview [propeller natural language processing development library]
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
How does Huawei enable debug and how to make an image port
黄金价格走势k线图如何看?
MySQL learning notes 2
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
Crawler request module
UE4 unreal engine, editor basic application, usage skills (IV)
ctf. Show PHP feature (89~110)
Folio.ink 免费、快速、易用的图片分享工具
Comments on flowable source code (XXXV) timer activation process definition processor, process instance migration job processor
竞赛题 2022-6-26
剑指 Offer 12. 矩阵中的路径
【Flask】响应、session与Message Flashing
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
[flask] official tutorial -part1: project layout, application settings, definition and database access
[flask] static file and template rendering
01.Go语言介绍