python刷LeetCode:28. 实现 strStr()

难度等级:简单

题目描述:

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-strstr
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路:

解法一:python字符串内置方法,但是这样刷算法失去意义。haystack.index(needle )或者 haystack.find(needle )

解法二:通过索引和haystack长度来遍历haystack,然后比较片段字符串和needle的值是否相等。

解题代码:

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        len_ned = len(needle)
        len_stak = len(haystack)
        j = 0
        for i in range(len_stak+1):  # 遍历haystack
            if i+len_ned < len_stak + 1:
                if haystack[i: i + len_ned] == needle:  # 判断needle字符串与haystack片段是否相等
                    j = i
                    break
            else:
                j = -1
                break
        return j

原文地址:https://www.cnblogs.com/jaysonteng/p/12375330.html

时间: 2024-08-02 06:35:32

python刷LeetCode:28. 实现 strStr()的相关文章

前端与算法 leetcode 28.实现 strStr()

# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和indexOf一样快),但我们还是要去了解api内实现的原理,在我们所熟悉的v8引擎中,indexOf使用了kmp和bm两种算法,在主串长度小于7时使用kmp,大于7的时候使用bm,bf咱就不说了哈,那个其实就是爆破算法, 提示 数据结构,kmp,bm 解析 kmp算法的核心其实就是动态规划,明确了

44. leetcode 28. Implement strStr()

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路:子串匹配,朴素匹配.

leetCode 28. Implement strStr() 字符串

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 在haystack中找与needle 第一个相匹配的位置.如果找不到,返回-1. 代码如下: class Solution { public:     int strStr(string haystac

[LeetCode] 28. Implement strStr() 实现strStr()函数

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",

&lt;每日 1 OJ&gt; -LeetCode 28. 实现 strStr()

题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回  -1. 示例 1: 输入: haystack = "hello", needle = "ll"输出: 2示例 2: 输入: haystack = "aaaaa", needle = "bba"输出: -1说明: 当 

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If

Leetcode 28.实现strStr() By Python

思路 如果不用python自带的索引功能,就要遍历的时候进行比较,用切片会很方便 可以偷个懒用python的索引功能 代码 class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ try: haystack.index(needle) return haystack.i

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

19.1.30 [LeetCode 28] Implement strStr()

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",