Leetcode题库——28.实现strStr()
@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()的更多相关文章
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- leetcode题库练习_数组中重复的数字
题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...
- 【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object): def strStr(se ...
- leetcode题库解答源码(python3)
下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- Leetcode题库——12.整数转罗马数字
@author: ZZQ @software: PyCharm @file: intToRoman.py @time: 2018/9/28 21:59 要求: 字符 数值 I 1 V 5 X 10 L ...
- LeetCode记录之28——Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode题库整理(自学整理)
1. Two Sum 两数之和 来源:力扣(LeetCode) 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利 ...
- 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 ...
随机推荐
- 一份可以落地靠谱iOS开发规范
列出来的都是个人觉得在团队合作,代码阅读,代码维护中比较重要的一些点,没有什么空格 间距华而不实的东西在里面.涉及 命名规范.编码规范.代码管理规范 命名规范 项目名都遵循大驼峰命名.例如:MSMob ...
- Math(初学)
package day01; public class Case12 { public static void main(String[] args) { System.out.println(Mat ...
- 阿里云服务器+宝塔环境+LNMP+tp5.1安装和调试
1.首先肯定得有个阿里云服务器,楼主是用来自己测试用得,所以就买了个1年得突发性实例,虽然网上都说别买这种,不过个人测试还是没有多少要求得 2.买了服务器之前可以顺带买个域名,再顺带去备案一下,阿里云 ...
- nRF52832 改变ATT_MTU提高蓝牙数据发送速率(nRF5_SDK_14.2.0)
nRF52832 作为一个低功耗蓝牙芯片,其数据发送发送速率一直都偏低(高就不叫低功耗了^_^),作为初学者在网上找了很多资料,终于找到通过修改ATT_MTU来提升发送速率的方法,最快能达到8.2KB ...
- 【HNOI2015】菜肴制作
题面 题解 这道题目首先可以想到拓扑排序,但是肯定不是字典序最小的排列. 比如说,有\(4\)种菜,限制为\(2 \to 4, 3 \to 1\),那么如果求字典序最小的排列会算出\((2, 3, 1 ...
- 【html】标签的分类
一.标签的分类 1. 块状元素 : 独占一行, 宽高,行高,顶底部边距都可以进行设置 <div> <p> <h1-h6> <ol> <ul> ...
- pandas:apply和transform方法的性能比较
1. apply与transform 首先讲一下apply() 与transform()的相同点与不同点 相同点: 都能针对dataframe完成特征的计算,并且常常与groupby()方法一起使用. ...
- [HNOI2012]永无乡 线段树合并
[HNOI2012]永无乡 LG传送门 线段树合并练手题,写这篇博客只是为了给我的这篇文章找个板子题. 并查集维护连通性,对于不在同一个连通块内的合并操作每次直接合并两颗线段树,复杂度\(O(n \l ...
- 2_C语言中的数据类型 (七)printf与scanf
1 字符串格式化输出和输入 1.1 字符串在计算机内部的存储方式 字符串是内存中一段连续的char空间,以’\0’结尾 “”是C语言表达字符串的方式 1.2 ...
- WPF中如何使用BusyIndicator
一.下载dll:http://wpftoolkit.codeplex.com/releases/view/99072 下载之后将WPFToolkit引用到WPF项目下: 二.添加命名空间: xmlns ...