@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. 在用AJAX跨域请求时遇到的问题

    刚刚接触ajax就遇到一个词--跨域. 在我百度了各种资料以后总结了一句话:“只要不是在一个协议.域.名端口下,都属于跨域(127.0.0.1本地也属于跨域)”. 在做ajax请求的时候,请求不到并且 ...

  2. 用windows或ubuntu访问apfs或mac分区

    MacBook读写不了NTFS,Windows也读写不了APFS和Mac OS 扩展分区,Ubuntu又用的是ext4分区,有时候想用U盘传点东西真的费劲.著名公司Paragon Software开发 ...

  3. PHP分行打印数组-php输出数组方法大全

    我们都知道php有两种方式可以打印数组 $arr = array( "a"=>"orange", "b"=>"bana ...

  4. CodeIgniter Doctrine2基本使用(一)(转)

    CodeIgniter Doctrine2基本使用(一) 之前写了一篇文章叫作<CodeIgniter 3.0整合Doctrine2>里面介绍了一些简单的Doctrine2的用法,当然我也 ...

  5. HDFS上传数据的流程

    1.当客户端输入一条指令:hdfs dfs -put text.txt /text时,这条命令会给到DistributeFileSystem. 2.通过DistributeFileSystem简称DF ...

  6. Cloudera Manager Server CDH 5.15部署

    安装前准备 主机和系统 Host OS Memory IP bigdata001-dev Cent OS 7.4 x64 32G 192.168.1.1 bigdata002-dev Cent OS ...

  7. TCP/IP协议、HTTP协议

    一.序: TCP/IP协议是程序开发的基础知识,我们都知道它可以实现不同计算机之间的通信,它是什么意思?怎么实现通信的? 二.TCP/IP协议: (1)协议:约定 (2)tcp/ip:tcp是传输控制 ...

  8. C++STL学习笔记_(2)deque双端数组知识

    #include<iostream> using namespace std; #include "deque" #include "algorithm&qu ...

  9. Java并发工具类(一):等待多线程完成的CountDownLatch

    作用 CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行 简介 CountDownLatch是在java1.5被引入的,存在于java.uti ...

  10. [webpack]--webpack 如何解析代码模块路径

    前言 webpack是如何解析代码模块路径 webpack 中有一个很关键的模块 enhanced-resolve 就是处理依赖模块路径的解析的,这个模块可以说是 Node.js 那一套模块路径解析的 ...