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 ...
随机推荐
- Linux之旅(1): diff, patch和quilt (下)
Linux之旅(1): diff, patch和quilt (下) 2 quilt 我们自己的项目能够用cvs或svn管理所有代码.但有时我们要使用其它开发人员维护的项目.我们须要改动一些文件,但又不 ...
- 子级Repeater获取 父级Repeater
第一种方法,子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container.NamingContainer.NamingContainer as Rep ...
- css控制父元素下的子元素自适应高度,且高度一致
css代码: .wrap{width:600px;margin:0 auto; overflow:hidden;} .left{background:#ccc;width:300px;float:le ...
- (二)Activity启动模式
一.标准模式(standrard) 1.当新建一个Activity时,默认情况下就是标准模式,也可以通过AndroidManifest文件显示指定其launchMode为standard <?x ...
- DBSCAN(Density-based spatial clustering of applications with noise)
Density-based spatial clustering of applications with noise (DBSCAN) is a data clustering algorithm ...
- 字符串聚合技术(String Aggregation Techniques)
from: http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php String Aggregation ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- mysql server advanced 5.6基于oracle linux 6.6的安装
mysql 安装有两种,rpm安装和源码包安装,两种包都可以从www.mysql.com官网下载,这次我测试下rpm安装方式. 1.安装环境以及mysql版本: 1.1vcenter 虚拟机环境 1. ...
- VirtualBox安装linux增强工具报错
错误提示: Building the OpenGL support module [FAILED] 解决办法 cd /media/VBOXADDITIO ...
- jQuery的实用小技巧
1. 禁止右键点击 $(function(){ $(document).bind('contextmenu', function(e){ return false; }) }) 2. 隐藏搜索文本框文 ...