当前位置:网站首页>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? ?

边栏推荐
- Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
- Mathematical modeling learning from scratch (2): Tools
- Superfluid_ HQ hacked analysis
- WGet: command line download tool
- Initialize MySQL database when docker container starts
- Leetcode skimming questions_ Sum of squares
- Internship: unfamiliar annotations involved in the project code and their functions
- How does the crystal oscillator vibrate?
- Basic operations of databases and tables ----- default constraints
- 【SSRF-01】服务器端请求伪造漏洞原理及利用实例
猜你喜欢

Leetcode skimming questions_ Sum of squares

National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!

Leetcode skimming questions_ Invert vowels in a string

Win10 add file extension

A Cooperative Approach to Particle Swarm Optimization

Basic operations of database and table ----- delete data table

How does the crystal oscillator vibrate?

Basic operations of databases and tables ----- non empty constraints
![[flask] official tutorial -part1: project layout, application settings, definition and database access](/img/c3/04422e4c6c1247169999dd86b74c05.png)
[flask] official tutorial -part1: project layout, application settings, definition and database access

3D模型格式汇总
随机推荐
Initialize MySQL database when docker container starts
[flask] response, session and message flashing
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
Flutter Doctor:Xcode 安装不完整
[the most complete in the whole network] |mysql explain full interpretation
Unity VR solves the problem that the handle ray keeps flashing after touching the button of the UI
Leetcode sword finger offer 59 - ii Maximum value of queue
selenium 元素定位(2)
LeetCode 322. Change exchange (dynamic planning)
TrueType字体文件提取关键信息
普通人下场全球贸易,新一轮结构性机会浮出水面
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
剑指 Offer 12. 矩阵中的路径
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Basic operations of databases and tables ----- non empty constraints
C web page open WinForm exe
【Flask】获取请求信息、重定向、错误处理
Cookie concept, basic use, principle, details and Chinese transmission
Leetcode skimming questions_ Verify palindrome string II
Leetcode 剑指 Offer 59 - II. 队列的最大值