ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

leetcode 28. Implement strStr()(python)

2021-10-20 11:05:51  阅读:194  来源: 互联网

标签:return python needle len strStr Implement haystack


描述

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Example 3:

Input: haystack = "", needle = ""
Output: 0

Note:

0 <= haystack.length, needle.length <= 5 * 10^4
haystack and needle consist of only lower-case English characters.

解析

根据题意,就是在 haystack 中找到第一次出现 needle 的索引,如果没有则返回 -1 ,如果 needle 为空字符串,则返回 0 。可以直接使用暴力的方法:

  • 当 needle 为空字符串,直接返回 0;
  • 遍历 haystack ,当以索引 i 为开头的字符串与 needle 相等的时候,即 haystack[i:i+len(needle)] == needle ,返回 i ;
  • 遍历结束,直接返回 -1 ;

我们看到上面 note 中的限制条件,haystack 和 needle 的长度最大有 50000 ,用暴力方法解决的话,本以为不会通过,没想到可以通过。其实还是不推荐,当长度更长的字符串出现就肯定会内存爆炸。

解答

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not needle:return 0
        for i in range(len(haystack)-len(needle)+1):
            if haystack[i:i+len(needle)] == needle:
                return i
        return -1

运行结果

Runtime: 292 ms, faster than 23.53% of Python online submissions for Implement strStr().
Memory Usage: 14.8 MB, less than 17.25% of Python online submissions for Implement strStr().

解析

既然暴力这种奇技淫巧都能拿出来,那么肯定可以想到用 python 的内置函数了,find 函数就可以满足题目要求,如果 needle 存在于 haystack.find ,那么就返回第一次出现的索引,否则就返回 -1 。但是这种方法我仍然不推荐,太 low ,体现不出一点技术含量。

解答

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not needle:return 0
        return haystack.find(needle);

运行结果

Runtime: 20 ms, faster than 69.53% of Python online submissions for Implement strStr().
Memory Usage: 13.7 MB, less than 69.35% of Python online submissions for Implement strStr().

原题链接:https://leetcode.com/problems/implement-strstr/

您的支持是我最大的动力

标签:return,python,needle,len,strStr,Implement,haystack
来源: https://blog.csdn.net/wang7075202/article/details/120861969

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有