作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/implement-strstr/description/

题目描述

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", needle = "bba"
Output: -1

题目大意

实现在haystack中找出needle第一次出现的位置,如果不存在,那么就返回-1.

解题方法

find函数

找出一个长串中小串的位置。这样太简单了。。

Python中,find()函数就是实现这个功能,如果找不到子串的话,返回-1.

另外,index()会在找不到的时候报错,这是两个函数的区别。

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

遍历+切片

这个题这么考就没意思了,自己实现了一下find函数。这里有个需要注意的点,i的变动范围是[0,M-N]闭区间,

时间复杂度是O(M),空间复杂度是O(1)。超过96%.

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

C++写法:

要注意的是string的substr方法第一个参数是起始位置,第二个参数是切片长度。

class Solution {
public:
int strStr(string haystack, string needle) {
int M = haystack.size();
int N = needle.size();
for (int i = 0; i < M - N + 1; i ++){
if (haystack.substr(i, N) == needle){
return i;
}
}
return -1;
}
};

日期

2018 年 2 月 4 日
2018 年 11 月 3 日 —— 雾霾的周六
2018 年 11 月 26 日 —— 11月最后一周!

【LeetCode】28. Implement strStr() 解题报告(Python)的更多相关文章

  1. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

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

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [leetcode]28. Implement strStr()实现strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. [LeetCode] 28. Implement strStr() ☆

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

随机推荐

  1. 34、在排序数组中查找元素的第一个和最后一个位置 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(34)在排序数组中查找元素的第一个和最后一个位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: / ...

  2. c#Gridview添加颜色

    e.Row.Cells[1].ForeColor = System.Drawing.Color.Blue;

  3. Spring同一个类中的注解方法调用AOP失效问题总结

    public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl ...

  4. jquery对radio和checkbox的操作

    jQuery获取Radio选择的Value值 代码  $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Valu ...

  5. oracle体系结构(图)

  6. 【Java基础】HashMap原理详解

    哈希表(hash table) 也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,本文会对java集合框架中Has ...

  7. 使用JDBCTemplate执行DQL/DML语句

    package cn.itcast.datasource.jdbctemplate;import cn.itcast.domain.User;import cn.itcast.utils.JDBCUt ...

  8. .net 5 开发跨平台客户端程序

    介绍下.net 跨平台开发服务端程序的过程, .net 5发布已经有段时间了,.net 5根据微软官方的说法将来只有一个.net版本,也就是不在有core之分.从.net5开始整合.net frame ...

  9. CF190C STL 题解

    * 题意 :给出只会出现 pair 和 int 的字符串 , 要求按照给出 pair 和 int 的顺序 , 添加 ' < '   ,   ' > '  ,  ' , ' 这三个符号 , ...

  10. last显示出unknown用户

    这问题是群里有朋友发了一张照片看到的: 出现问题第一时间当然是百度或者谷歌,结果还是查到了,原来是gdm作怪,也可以认为是bug 该用户由GDM创建(可能是由于错误).并不是真的有"未知&q ...