当前位置:网站首页>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? ?
边栏推荐
- Yii console method call, Yii console scheduled task
- Leetcode 剑指 Offer 59 - II. 队列的最大值
- Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
- Ordinary people end up in Global trade, and a new round of structural opportunities emerge
- You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
- [flask] official tutorial -part1: project layout, application settings, definition and database access
- Leetcode 208. 实现 Trie (前缀树)
- 02.Go语言开发环境配置
- XSS learning XSS lab problem solution
- Leetcode1961. 检查字符串是否为数组前缀
猜你喜欢
黄金价格走势k线图如何看?
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
电气数据|IEEE118(含风能太阳能)
leetcode刷题_平方数之和
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
[Jiudu OJ 09] two points to find student information
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Initialize MySQL database when docker container starts
ORA-00030
随机推荐
Huawei converged VLAN principle and configuration
[le plus complet du réseau] | interprétation complète de MySQL explicite
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
剑指 Offer 38. 字符串的排列
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
[flask] official tutorial -part2: Blueprint - view, template, static file
500 lines of code to understand the principle of mecached cache client driver
【已解决】如何生成漂亮的静态文档说明页
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
ORA-00030
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
Leetcode 208. 实现 Trie (前缀树)
Unity | two ways to realize facial drive
晶振是如何起振的?
【Flask】静态文件与模板渲染
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
[flask] static file and template rendering
Threedposetracker project resolution
3D model format summary
[flask] response, session and message flashing