当前位置:网站首页>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? ?
边栏推荐
- ctf. Show PHP feature (89~110)
- How to see the K-line chart of gold price trend?
- 剑指 Offer 38. 字符串的排列
- 什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
- 黄金价格走势k线图如何看?
- National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
- Yii console method call, Yii console scheduled task
- [le plus complet du réseau] | interprétation complète de MySQL explicite
- leetcode刷题_反转字符串中的元音字母
- False breakthroughs in the trend of London Silver
猜你喜欢
现货白银的一般操作方法
ORA-00030
晶振是如何起振的?
[detailed] several ways to quickly realize object mapping
[Jiudu OJ 09] two points to find student information
Idea sets the default line break for global newly created files
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Basic operations of databases and tables ----- non empty constraints
c#网页打开winform exe
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
随机推荐
竞赛题 2022-6-26
A Cooperative Approach to Particle Swarm Optimization
【全網最全】 |MySQL EXPLAIN 完全解讀
Redis-字符串类型
安装Redis
2022年PMP项目管理考试敏捷知识点(8)
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
【已解决】如何生成漂亮的静态文档说明页
ClickOnce 不支持请求执行级别“requireAdministrator”
3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
MATLB|实时机会约束决策及其在电力系统中的应用
Threedposetracker project resolution
【详细】快速实现对象映射的几种方式
[flask] static file and template rendering
普通人下场全球贸易,新一轮结构性机会浮出水面
Leetcode 208. Implement trie (prefix tree)
剑指 Offer 38. 字符串的排列
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
[flask] official tutorial -part2: Blueprint - view, template, static file