@author: ZZQ

@software: PyCharm

@file: strStr.py

@time: 2018/11/6 20:04

要求:给定一个 haystack 字符串和一个 needle 字符串,

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

示例 1:

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

输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"

输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。

这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

class Solution():
def __init__(self):
pass def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
len_h = len(haystack)
len_n = len(needle)
if len_n == 0 or needle == "" :
return 0
if len_h == 0 or haystack == "" or len_n > len_h:
return -1
index_h = 0
while index_h < len_h:
temp_index = index_h
first_index = index_h
match_t = 0
for i in range(len_n):
if temp_index == len_h:
return -1
if haystack[temp_index] != needle[i]:
break
temp_index += 1
match_t += 1
if match_t == len_n:
return first_index
else:
index_h += 1
return -1 def strStr2(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
len_h = len(haystack)
len_n = len(needle)
if len_n == 0 or needle == "":
return 0
if len_h == 0 or haystack == "" or len_n > len_h:
return -1
index_h = 0
while index_h < len_h:
temp_h = index_h
index_n = 0
while haystack[temp_h] == needle[index_n]:
temp_h += 1
index_n += 1
if index_n == len_n:
return index_h
if temp_h == len_h:
return -1
index_h += 1
return -1

Leetcode题库——28.实现strStr()的更多相关文章

  1. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  2. leetcode题库练习_数组中重复的数字

    题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...

  3. 【leetcode❤python】 28. Implement strStr()

    #-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(se ...

  4. leetcode题库解答源码(python3)

    下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...

  5. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  6. Leetcode题库——12.整数转罗马数字

    @author: ZZQ @software: PyCharm @file: intToRoman.py @time: 2018/9/28 21:59 要求: 字符 数值 I 1 V 5 X 10 L ...

  7. LeetCode记录之28——Implement strStr()

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

  8. LeetCode题库整理(自学整理)

    1. Two Sum 两数之和       来源:力扣(LeetCode) 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利 ...

  9. LeetCode题库13. 罗马数字转整数(c++实现)

    问题描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II  ...

随机推荐

  1. n进制转十进制

    #include<cstdio> #include<iostream> using namespace std; ; int main(){ ,len=; char ch[ma ...

  2. 时间序列分析工具箱——timetk

    目录 时间序列分析工具箱--timetk timetk 的主要用途 加载包 数据 timetk 教程: PART 1:时间序列机器学习 PART 2:转换 翻译自<Demo Week: Time ...

  3. 用NI的数据采集卡实现简单电子测试之4——半导体温度传感器

    本文从本人的163博客搬迁至此. 为了展示NImax(Measurement & Automation explorer)的强大配置功能,做了一个半导体温度传感器测试的示例. 一.半导体温度传 ...

  4. Truck History(prime)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31871   Accepted: 12427 D ...

  5. 2018下学期C语言学习总结

    怎么说呢,2018年下学期已经过去了,2019年的上学期又来了,在2018年没有达到自己想要的结果,希望2019年可以达到,加油!我希望我以后可以又快又准确的完成C语言的编程,学习好所以关于电脑的基础 ...

  6. 2-5 re模块练习题

    1 练习: 1.验证手机号是否合法 2.验证邮箱是否合法 3.开发一个简单的python计算器,实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2 ...

  7. 4542: [Hnoi2016]大数

    4542: [Hnoi2016]大数 链接 分析: 如果p等于2或者5,可以根据最后一位直接知道是不是p的倍数,所以直接记录一个前缀和即可. 如果p不是2或者5,那么一个区间是p的倍数,当且仅当$\f ...

  8. cogs1685 【NOI2014】魔法森林 Link-Cut Tree

    LCT练手好题啊. SPFA的做♂FA是把边按照a排序,然后加一条权值为b的边跑SPFA,不断更新答案.很好的做♂FA,但复杂度无♂FA保证. LCT的做♂FA类似,也是把边按照a排序,然后也是加一条 ...

  9. JAVA 删除指定目录下指定文件类型的所有文件

    public class DelFile { public static void main(String[] args) { File file = new File("C:\\DETEC ...

  10. xshell提示评估过期怎么办?

    刚刚打开xshell准备连上虚拟机写代码,结果提示评估过期,真的很气,在百度上找了好久才找到解决办法,现在分享给大家:. 1.复制这个链接在浏览器打开https://www.netsarang.com ...