问题描述:

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

方法1:直接用find方法

 class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
index = haystack.find(needle)
if index >= 0:
return index
else:
return -1

方法2:用截取靶串的方式做对比

 class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
len1=len(haystack)
len2=len(needle)
for i in range(len1-len2+1):
if haystack[i:i+len2]==needle:
return i
return -1

2018-07-23 18:10:11

LeetCode--028--实现strSTR()的更多相关文章

  1. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

  3. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  4. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  6. 028 Implement strStr() 实现 strStr()

    实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...

  7. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...

  8. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  10. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

随机推荐

  1. mysql日志文件目录

    默认情况下mysql的二进制日志文件保存在默认的数据目录data下,如:/usr/local/mysql/data 修改日志保存目录(/backup/mysqlbinlog/mysql-bin)的话: ...

  2. 如何使用Unity制作虚拟导览(一)

    https://www.cnblogs.com/yangyisen/p/5108289.html Unity用来制作游戏已经是目前市场上的一个发展趋势,而且有越来越多的公司与开发者不断的加入,那么Un ...

  3. 实用的4~20mA输入/0~5V输出的I/V转换电路(转)

    源: 实用的4~20mA输入/0~5V输出的I/V转换电路

  4. Android JSON 解析关键代码

    Android Json 解析其实还是蛮重要的知识点,为什么这么说呢,因为安卓通信大部分的协议都是使用 json 的方式传输,我知道以前大部分是使用的 xml ,但是时代在发展社会在进步,json 成 ...

  5. 20145127《java程序设计》第一次实验

    <java程序设计>第一次实验 实验内容及其步骤 1.使用JDK编写简单的Java小程序: Java编译的方法有很多,最基础最简单的就是使用命令行,记事本,Java虚拟机直接进行编译,下面 ...

  6. 20145227鄢曼君《网络对抗》MSF基础应用

    20145227鄢曼君<网络对抗>MSF基础应用 主动攻击:ms08_067漏洞攻击实践 两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版).在VMware中设置 ...

  7. CSS布局总结及实际应用中产生的问题

    布局初步 所谓布局,其实是指的将网页内容以一定的方式放到合适的位置上去. 布局的基本步骤: 1, 将“当前版面”以视觉上界限明显的方式进行划分若干个区块,划分只用两种方式: a) 上下结构:此时,只要 ...

  8. 嵌入式系统 Boot Loader 技术内幕【转】

    本文转载自:https://www.ibm.com/developerworks/cn/linux/l-btloader/ 本文详细地介绍了基于嵌入式系统中的 OS 启动加载程序 ―― Boot Lo ...

  9. UDP的socketasynceventargs

    C# 使用 SocketAsyncEventArgs 实现UdpSocket系列 http://www.cnblogs.com/zwq194/archive/2012/10/30/2746393.ht ...

  10. DPDK的安装与绑定网卡

    DPDK的安装有两种方法: 第一种是使用dpdk/tools/setup.sh选择命令字来安装:第二种是自己手动安装.为了更好地熟悉DPDK,我使用第二种方法. 0.设定环境变量 export RTE ...