public class Solution {
public int StrStr(string haystack, string needle) {
return haystack.IndexOf(needle);
}
}

https://leetcode.com/problems/implement-strstr/#/description

python实现,不实用内置函数:

 class Solution:
def strStr(self, haystack: str, needle: str) -> int:
n = len(haystack)
m = len(needle)
if m == :
return
if n == or m > n:
return -
if haystack == needle:
return i,j = ,
idx = i
while i < n and j < m:
if haystack[i] != needle[j]:
i = idx +
idx = i
j =
else:
if idx == -:
idx = i
i +=
j +=
if j == m:
return idx
return -

作为一道easy题目,提交成功率只有33%,可以看出本题的一些细节需要仔细分析,否则很容易出错。

leetcode28的更多相关文章

  1. LeetCode28 Implement strStr()

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

  2. [Swift]LeetCode28. 实现strStr() | Implement strStr()

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

  3. leetcode-28.实现strStr()

    leetcode-28.实现strStr() 题意 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字 ...

  4. LeetCode28.实现strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  5. LeetCode28.实现strStr() JavaScript

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  6. LeetCode28——实现strStr()

    6月中下旬辞职在家,7 月份无聊的度过了一个月.8 月份开始和朋友两个人写项目,一个后台和一个 APP ,APP 需要对接蓝牙打印机.APP 和蓝牙打印机都没有搞过,开始打算使用 MUI 开发 APP ...

  7. leetcode28 strstr kmp bm sunday

    字符串匹配有KMP,BM,SUNDAY算法. 可见(https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa- ...

  8. 【剑指offer28:字符串的排列】【java】

    题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. import ja ...

随机推荐

  1. hdu4280网络流之dinic

    这题就是个模板题,不过我是第一次写dinic,好久没用链式前向星又不会了... 时间:9126ms #include<map> #include<set> #include&l ...

  2. Intel IDEA 2018破解(亲测成功)

    破解网址:https://jingyan.baidu.com/article/cb5d6105d9b1b1005d2fe074.html

  3. 使用Jenkins来实现内部的持续集成流程(上)

    前言 Jenkins和TeamCity都是大杀器,用于搭建内部持续集成环境都是妥妥的.本篇主要介绍Jenkins的安装,下篇将介绍相关配置和使用. 目录 安装和配置 第一次启动 插件安装,第一次进入时 ...

  4. mysql插入中文数据报错 java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column

    1.我们创建数据库的时候没有更改数据库的字符集为utf8. 在mysql工具中,右击数据库,->"改变数据库",->选择“基字符集”为utf-8; 2,数据库中表的字符 ...

  5. 探究JS中对象的深拷贝和浅拷贝

    深拷贝和浅拷贝的区别 在讲深拷贝和浅拷贝的区别之前,回想一下我们平时拷贝一个对象时是怎么操作的?是不是像这样? var testObj1 = {a: 1, b:2}, testObj2=testObj ...

  6. DateTime和字符串转换问题

    DateTime和string之间的相互转换经常碰到,可就这么简单的一个转换其中也有些需要注意的地方. static void Main(string[] args) { string format ...

  7. java回收finalize方法的作用(编程思想)

    清理:终结处理和垃圾回收 java有垃圾回收期负责回收无用对象占据的内存资源.但也有这种情况:假定你的对象(并非使用new)获得了一块“特殊”的内存区域,由于垃圾回收期只知道释放那些由new分配的内存 ...

  8. 如何将Virtualbox和VMware虚拟机相互转换[译文211] - 转

    迁移到其他的虚拟机程序可行会吓倒一批人.如果你已经按照自己的喜好设置好了虚拟机,那么就不需要再从头安装——你可以迁移现有的虚拟机. VirtualBox 和 VMware 使用不同的虚拟机格式,不过他 ...

  9. Webroot SecureAnywhere AntiVirus 2014 – 免费6个月

    Webroot SecureAnywhere 是由webroot推出的一款云安全软件,除了能够清除病毒外,特点是体积小.强力查杀木马.间谍软件.Rootkit 等等,为你的个人私隐信息提供全面的保护. ...

  10. New Concept English Two 19 49

    $课文47 嗜酒的鬼魂 481. A public house which was recently bought by Mr.Ian Thompson is up for sale. 伊恩.汤普森先 ...