当前位置:网站首页>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? ?
边栏推荐
- Maya hollowed out modeling
- Leetcode skimming questions_ Sum of squares
- Dynamics 365 开发协作最佳实践思考
- 【全網最全】 |MySQL EXPLAIN 完全解讀
- Loop structure of program (for loop)
- ThreeDPoseTracker项目解析
- You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
- Leetcode skimming questions_ Verify palindrome string II
- LeetCode 322. Change exchange (dynamic planning)
- 【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
猜你喜欢
随机推荐
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
Format code_ What does formatting code mean
ORA-00030
XSS learning XSS lab problem solution
DOM introduction
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
[flask] static file and template rendering
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
Unity | 实现面部驱动的两种方式
Leetcode sword finger offer 59 - ii Maximum value of queue
A Cooperative Approach to Particle Swarm Optimization
1. Introduction to basic functions of power query
Yii console method call, Yii console scheduled task
Leetcode skimming questions_ Invert vowels in a string
500 lines of code to understand the principle of mecached cache client driver
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
Condition and AQS principle
竞价推广流程
Leetcode 208. Implement trie (prefix tree)
Flutter Doctor:Xcode 安装不完整