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 ...
随机推荐
- 数据分页jdbc+mysql实现
通过简单粗糙的功能不完善的客户管理案例体现jdbc+mysql的数据分页,与其说是管理系统,不如说就是一个jdbc数据分布的demo而已.但是话又说回来,麻雀虽小,五脏俱全.虽然是个小demo,但是其 ...
- CentOS6.8下完全干净卸载mysql
来源整理于 https://www.cnblogs.com/wanghuaijun/p/6398240.html 虚拟机CentOS6.8下 先执行命令查看目录是否存在mysql 文件夹: cd ...
- PostgreSQL Replication之第四章 设置异步复制(4)
4.4 基于流和基于文件的恢复 生活并不总只是黑色或白色:有时也会有一些灰色色调.对于某些情况下,流复制可能恰到好处.在另一些情况下,基于文件复制和PITR是您所需要的.但是也有许多情况下,您既需要流 ...
- python import windows文件路经
import sys sys.path.append("E:\\python\\workspacepython\\PY001\\src\\testpy01") import str ...
- mac本 maven项目还没发布成功,tomcat就报没有监听ContextLoaderListener 的解决方法
Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderL 严重: Error config ...
- dstat---统计磁盘,CPU,IO,等相关信息
dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一个全能系统信息统计工具.与sysstat相比,dstat拥有一个彩色的界面,在手 ...
- Unity shader 代码高亮+提示
Shader Unity Support This is Unity CG Shaders Support. It has code completion support and uses C/C++ ...
- ECNUOJ 2616 游黄山
游黄山 Time Limit:1000MS Memory Limit:65536KB Total Submit:165 Accepted:52 Special Judge Description Po ...
- ECNUOJ 2856 仰望星空
仰望星空 Time Limit:1000MS Memory Limit:65536KBTotal Submit:373 Accepted:145 Description 我仰望星空, 它是那样辽阔而 ...
- Nutch1.6学习笔记
回 到 目 录 暑假每天傍晚或晚上更新 伪恋赛高 这里提供nutch1.6的src下载: apache-nutch-1.6-src.zip 115网盘礼包码:5lbcymlo6u76http://11 ...