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. 【Demo】Tree.js实例

    Three.js是通过对WebGL接口的封装与简化而形成的一个易用的图形库. 简单点的说法:WebGL可以看成是浏览器给我们提供的接口,在javascript中可以直接用这些API进行3D图形的绘制: ...

  2. linux yum安装lsof命令

    [root@ITC-MCC ~]# yum install lsof[USM] permission denied^C[root@ITC-MCC ~]# [root@ITC-MCC ~]# [root ...

  3. BZOJ1555 KD之死

    如果没有必选的限制条件,就是水题了... 只要按照w + t排序就可以了,然后搞个堆来维护 于是有了限制条件,还是水题... 到了必选的时候强制选上,不加入堆中即可. /*************** ...

  4. 048——VUE中使用animate.css动画库控制vue.js过渡效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. SGU 156 Strange Graph 欧拉回路,思路,汉密尔顿回路 难度:3

    http://acm.sgu.ru/problem.php?contest=0&problem=156 这道题有两种点 1. 度数>2 在团中的点,一定连接一个度数为2的点 2. 度数等 ...

  6. Python IDE集成开发工具

    Python IDE集成开发工具 Python IDE 本文为大家推荐几款款不错的 Python IDE(集成开发环境),比较推荐 PyCharm,当然你可以根据自己的喜好来选择适合自己的 Pytho ...

  7. 如何创建管理员权限的CMD命令提示符窗口

    最近在使用netstat -anob命令时提示 请求的操作需要提升. 总结了几种创建管理员权限的CMD命令行的方法. 创建临时管理员权限的CMD Win8系统: 按下windows徽标,直接输入cmd ...

  8. 关于面向对象和String类型的 09,10

    package test.面试题; public class Test9 { public static void main(String[] args){ Outer.Inner in=new Ou ...

  9. New Concept English Two 3

    $课文5 无错号之虞 47. Mr.James Scott has a garage in Silbury and now he has just bought another garage in P ...

  10. 阿里云windows时间同步服务地址

    偶然发现的, 记录一下 ntp1.aliyun.com