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 ...
随机推荐
- Oracle数据库Helper类
using System;using System.Collections.Generic;using System.Data;using System.Data.OleDb;using System ...
- softwares我的软件列表(转载)
[TOC] 个人在archlinux下的日常使用经验列出,故而在archlinux及其衍生发行版中,以下所列软件几乎可以从archlinux官方源或者aur中搜索下载安装,所列出名字一般即是其包名,使 ...
- SSD-2(代码部分介绍)
single shot multibox detectior tensorflow 代码 一.SSD重要参数设置 在ssd_vgg_300.py文件中初始化重要的网络参数,主要有用于生成默认框的特征层 ...
- [洛谷P3932]浮游大陆的68号岛
题目大意:有一行物品,每两个物品之间有一个距离.每个物品有一个价值.现在问你若干问题,每个问题问你把l~r所有物品全部搬到物品x处需要多少价值. 把物品a搬到物品b处的价值为物品a的价值乘a到b的距离 ...
- MPI对道路车辆情况的Nagel-Schreckenberg 模型进行蒙特卡洛模拟
平台Ubuntu 16.04,Linux下MPI环境的安装见链接:https://blog.csdn.net/lusongno1/article/details/61709460 据 Nagel-Sc ...
- 【Codeforces Round #239 (Div. 1) B】 Long Path
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] DP,设f[i]表示第一次到i这个房间的时候传送的次数. f[1] = 0,f[2] = 2 考虑第i个位置的情况. 它肯定是从i- ...
- 【Round #36 (Div. 2 only) C】Socks Pairs
[题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...
- 一 梳理 从 HDFS 到 MR。
MapReduce 不仅仅是一个工具,更是一个框架.我们必须拿问题解决方案去适配框架的 map 和 reduce 过程 很多情况下,需要关注 MapReduce 作业所需要的系统资源,尤其是集 ...
- ArcGIS api for javascript——图形-选择一个范围内的点
描述 本例展示了如何使用图形显示查询结果,如何使用draw toolbar在地图上选择图形和如何修改图形符号为“高亮”. 在地图上画一个矩形区域,加亮矩形范围内的城市.这个应用统计高亮的城市个数并列出 ...
- openGl超级宝典学习笔记 (2) 7个主要的几何图元
点(GL_POINTS): 点总是正方形的像素,默认情况下,点的大小不受透视除法影响. 即无论与视点的距离怎样,它的大小都不改变.为了获得圆点.必须在抗锯齿模式下绘制点. 能够用glPointSize ...