LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr()
原题
实现字符串子串匹配函数strStr()。
假设字符串A是字符串B的子串。则返回A在B中首次出现的地址。否则返回-1。
注意点:
- 空字符串是全部字符串的子串,返回0
样例:
输入: haystack = “abc”, needle = “bc”
输出: 1
输入: haystack = “abc”, needle = “gd”
输出: -1
解题思路
字符串匹配常见的算法是KMP。只是感觉该算法理解困难,效率也不是特别高。
我用了Sunday算法来实现字符串的匹配。大体思路例如以下:
被搜索的字符串是”abcdefg”,要搜索的字符串是”ef”
abcdefg
ef
假设当前不匹配,则推断当前尝试匹配的后一位。即”c”是否在要搜索的字符串中,假设不在,则要搜索的字符串直接后移它自己的长度+1。
abcdefg
ef
假设存在,如此时”f”在”ef”中,则把该位置对齐。
abcdefg
ef
匹配成功返回结果。
AC源代码
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
if not haystack:
return -1
i = 0
needleLength = len(needle)
while i < len(haystack):
if haystack[i:i + needleLength] == needle:
return i
else:
index = 0
try:
index = needle.rindex(haystack[i + needleLength])
except Exception:
i += needleLength + 1
i += needleLength-index
return -1
if __name__ == "__main__":
assert Solution().strStr("abcdefg", "ab") == 0
assert Solution().strStr("abcdefg", "bc") == 1
assert Solution().strStr("abcdefg", "cd") == 2
assert Solution().strStr("abcdefg", "fg") == 5
assert Solution().strStr("abcdefg", "bcf") == -1
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源代码。
LeetCode Implement strStr()(Sunday算法)的更多相关文章
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
- 第3章:LeetCode--算法:strStr KMP算法
https://leetcode.com/problems/implement-strstr/ 28. Implement strStr() 暴力算法: int ViolentMatch(char* ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
随机推荐
- python中对单例模式的理解
class Foo(object): instance = None def __init__(self): pass def process(self): ' @classmethod #版本1单例 ...
- CUDA学习笔记(三)
近期对CUDA的理解,是对库的利用. 1 nvcc.exe是CUDA C编译器. 2 cudart.dll是CUDA运行时API动态链接库. 3 在Sdk目录下,\lib有CUTIL.CUDPP等函数 ...
- Python json数据中文输出问题。
这个问题困扰了我好久好久,最后看了一眼官方文档,解决问题了. 问题描述:从web上获取的json数据,然后对应的保存到了python的类型中.再次输出这个数据时,中文总会变成\u1234这种形式. P ...
- python ftp
#fpt_server.py#__*__ encoding=utf-8 __*__ import socket ,os class MyClass(object): def __init__(self ...
- SQLServer中同义词Synonym的用法
以前一直认为SqlServer中的同义词(Synonym)没有什么用处,所以也一直没有去查它的语法格式.今天碰到一个问题,用Synonym来解决再好不过了.问题是这样子的,我的系统中用到了多个数据库, ...
- WinDBG help
WinDBG is a great, free tool. It is more powerful than Visual Studio's built-in debugger, but is har ...
- 操作系统——第五章 输入输出(I/O)管理
这就是SDT表和DCT表
- js001 ---- async
Node.js异步流,详细见https://caolan.github.io/async/docs.html#parallel 1, async 用的比较多的是 waterfall, 瀑布流, 就是每 ...
- man帮助
man命令是Linux下的帮助指令,通过man指令可以查看Linux中的指令帮助.配置文件帮助和编程帮助等信息.
- C# 从需要登录的网站上抓取数据
[转] C# 从需要登录的网站上抓取数据 背景:昨天一个学金融的同学让我帮她从一个网站上抓取数据,然后导出到excel,粗略看了下有1000+条记录,人工统计的话确实不可能.虽说不会,但作为一个学计算 ...