28. Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
1
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
return haystack.find(needle)
2 brute force
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
result = -1
l_needle = len(needle)
l_haystack = len(haystack)
if l_needle == 0 and l_haystack == 0:
return 0
if l_haystack == 0 and l_needle!=0:
return result
if l_needle == 0 :
return 0
for index in range(l_haystack):
if l_haystack - index >= l_needle and needle == haystack[index:index+l_needle]:
result = index
break
return result
3 hash 有待改进
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
result = -1
l_needle = len(needle)
l_haystack = len(haystack)
if l_needle == 0 and l_haystack == 0:
return 0
if l_haystack == 0 and l_needle!=0:
return result
if l_needle == 0 :
return 0
if l_needle>l_haystack:
return result
hash_needle=hash(needle)
list_haystack=[]
for index in range(l_haystack):
if index<=(l_haystack-l_needle):
list_haystack.append(hash(haystack[index:index+l_needle]))
result=list_haystack.index(hash_needle) if hash_needle in list_haystack else -1
return result
28. Implement strStr()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- IPC-->PIPO
Programing python 4th page 228 """ IPC http://www.cnblogs.com/BoyXiao/archive/2011/01 ...
- WebApi 通过类名获取类并实例化
环境:Web Api 返回值类型为DTO实体,继承基类DTO---BaseResultDto public class BaseResultDto { /// <summary> /// ...
- 【转】CentOS系统中常用查看日志命令
来源:http://www.centoscn.com/CentOS/help/2014/0310/2540.html Linux IDE RedHat 防火墙活动 .cat tail -f 日 志 文 ...
- 苹果版App开发心得
这几个月中做的工作包括网站开发.安卓App开发和苹果App开发,前两者用的语言都是我熟悉的java,故苹果知识的学习,较安卓知识的学习,多出「语言基础」一块,其他方面差不多. 之前发过安卓那篇,如感兴 ...
- Intellij Idea 14 使用jetty-maven-plugin配置运行web工程
在项目中接触,虽然比较简单,也是经验的积累,web工程使用maven管理和构建,IDEA也是新接触的开发工具,用了一段时间,感觉so nice! 1:Run->Edit Configuratio ...
- java 使用 集合 制作学生管理系统
以上是文件组织结构 下面是个.java的具体代码: package com.collection.students.pojo; public class Student { private Strin ...
- 【译】Import Changes from Direct3D 11 to Direct3D 12
译者:林公子 出处:木木的二进制人生 转载请注明作者和出处,谢谢! 这是微软公布的Direct3D 12文档的其中一篇,此翻译留作学习记录备忘,水平有限,错漏难免,还望海涵. 原文链接是https:/ ...
- java htmlunit 抓取网页数据
WebClient webClient=new WebClient(BrowserVersion.CHROME); webClient.setJavaScriptTimeout(5000); webC ...
- Linux tcpdump命令详解
tcpdump官网:http://www.tcpdump.org/ 转载于:http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.ht ...
- three.js初涉略(一)
<!-- 最近要研究一下webgl,发现了webgl中文网(http://www.hewebgl.com/article/articledir/1).边研究教程边做下记录 --> thre ...