leetcode implement strStr python
#kmp
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(haystack) <= 0 and len(needle)<=0:
return 0 arrNext=self.getNext(needle)
i=0
j=0
intHLen=len(haystack)
intNLen=len(needle)
while i < intHLen and j < intNLen:
if j==-1 or haystack[i] == needle[j]:
i+=1
j+=1
else:
j=arrNext[j]
if j == intNLen:
return i-j
else:
return -1
def getNext(self,needle):
arrNext=dict()
arrNext[0]=-1
k=-1
j=0
intLen=len(needle)
while j < intLen:
if k==-1 or needle[k] == needle[j]:
k+=1
j+=1
arrNext[j]=k
else:
k=arrNext[k]
return arrNext
leetcode implement strStr python的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 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() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
随机推荐
- odbc连接数据库
using System; using System.Collections.Generic; using System.Text; using Console = System.Console; u ...
- C#根据函数名称执行对应的函数
using System; using System.Collections.Generic; using System.Reflection; namespace test { public cla ...
- 使用ADO.net中的链接字符串
需要引用:System.Configuration命名空间 ConfigurationManager.ConnectionStrings["sqlConnStr"].Connect ...
- Linux 重定向
Linux 标准文件描述符 描述符 缩写 描述 0 STDIN 标准输入 1 STDOUT 标准输出 2 STDERR 标准错误 3-9 应该是扩展的标准输出(待验证) 命令行重定 ...
- NSRangeFromString(<#NSString * _Nonnull aString#>) 和rangeOfString
NSRangeFromString NSString *str1 = @"abcdef"; NSString *str2 = @"1-105"; NSStrin ...
- apt-get命令学习
在windows下安装软件,我们只需要有EXE文件,然后双击,下一步直接OK就可以了.但在LINUX下,不是这样的.每个LINUX的发行版,比如UBUNTU,都会维护一个自己的软件仓库,我们常用的几乎 ...
- mysql学习(十二)内置函数
常用的内置函数,常用select\ 字符串函数 contat('' , '', .....) //连接字符串 select concat(name, ' age is ', age) from per ...
- hibernate中先建表还是先建实体类
在实际工作中往往是先建表然后再生成类原因:建好数据库表之后往往要对数据表进行一些优化,比如说建索引,比如说建中间表,比如建视图.如果先建类的话这些优化是无法生成的
- JS判断是否在微信浏览器打开
if (browser.versions.mobile) {//判断是否是移动设备打开.browser代码在下面 var ua = navigator.userAgent.toLowerCase(); ...
- Robot Framework语法学习(一)
Robot Framework语法学习: 一.变量的声明.赋值与使用 1.变量标识符:每个变量都可以用 变量标识符 ${变量名} 来表示. 2.变量声明:可以在TestSuite上点右键或者在Edi ...