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 ...
随机推荐
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- BZOJ 1069 求凸包+旋转卡壳
思路: 求凸包: 先按照x轴排个序 从左往右扫一遍 找到上凸壳 (用叉积) 再从右往左扫一遍 求下凸壳 搞个旋转卡壳就好啦~ 嗯 我手懒 用的C++ Complex库 巨好用! //By Sirius ...
- Idea的一些调试技巧及设置todo
程序员的工作内容,除了大部分时间写代码之外,因为有不少的时间是用在调试代码上.甚至说不是在调试代码,就是即将调试代码. :) 今天我们来谈谈调试代码的一些技巧,在使用IDE提供的debugger时一些 ...
- sqoop配置安装以及导入
安装sqoop的前提是已经具备java和hadoop的环境 1.上传并解压 (要导mysql的数据)得加入mysql的jdbc驱动包 接下来验证启动 Sqoop的数据导入 “导入工具”导入单个表从RD ...
- win10 的MQTT + apache-apollo服务器使用
我的使用环境是windows10 2.下载文件目录(注意:开始看教程说直接打开bin目录下的apollo.cmd文件,闪退,原因是没有java_home环境,必须添加java环境): 3.安装好jav ...
- [HAOI2007]理想的正方形 单调队列 暴力
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; #d ...
- 用Python爬取影视网站,直接解析播放地址。
记录时刻! 写这个爬虫主要是想让自己的爬虫实用,把脚本放到了服务器,成为可随时调用的接口. 思路算是没思路吧!把影视名带上去请求影视网站,然后解析出我们需要的播放地址. 我也把自己的接口分享出来.接口 ...
- JDK工具(一)–Java编译器javac
1.概述 javac.exe: Java编译器,将Java源代码转换成字节码. 2.用法 javac <选项> <源文件> (使用过程中发现,javac <源 ...
- ArcGIS api for javascript——图层-创建WMS图层类型的图层
本例使用一个WMS端点创建了一个简单的动态图层.首先,代码声明一个新的类my.CityStatesRiversUSAWMSLayer,该类继承esri.layers.DynamicMapService ...
- Equals和==比較
总结一下: 对于字符串来说.两个比較的都是对象的值,而且是等效的,这是由于MS重写了==运算符和Equals方法所致 对于非字符串的其它引用类型(非匿名类型)两个比較的都是对象 ...