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 ...
随机推荐
- nginx负载均衡及配置
nginx负载均衡及配置 1 负载均衡概述 负载均衡由来是因为当一台服务器单位时间内的访问量很大时,此时服务器的压力也会很大,当超过自身承受能力时,服务器就会崩溃.为避免让服务器崩溃,用户拥有更好的体 ...
- C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点
1 首先介绍几个常用到的转义符 (1) 换行符“\n”, ASCII值为10: (2) 回车符“\r”, ASCII值为13: (3) 水平制表符“\t”, ASCII值为 9 ...
- Scala(三):类
类:Class 1.简单类和无参方法 2.带getter和setter属性 3.只带getter属性 4.对象私有字段 5.Bean属性 6.辅助构造器 7.主构造器 8.嵌套类 1.简单类和无参方法 ...
- JavaWeb基础—Servlet重要对象
一.ServletConfig对象 当servlet配置了初始化参数后(<init-param> <param-name> <param-value>),web容器 ...
- SSM整合思路
引自网友: https://zhuanlan.zhihu.com/p/23917781
- mysql 5.7.12 winx64安装配置方法图文教程
这篇文章主要为大家分享了mysql 5.7.12winx64安装配置方法图文教程,感兴趣的朋友可以参考一下 之前安装mysql时未做总结,换新电脑,补上安装记录,安装的时候,找了些网友的安装记录, ...
- 20145209刘一阳《JAVA程序设计》第八周课堂测试
第八周课堂测试 1.下面代码中共有(C)个线程? public class ThreadTest { public static void main(String args[]){ MyThread ...
- C#调整图片亮度和对比度
BitmapSource bitmap = null; ; ; private void SetBrightness(int degree) { degree = degree * / ; Write ...
- luogu1117 [NOI2016]优秀的拆分
luogu1117 [NOI2016]优秀的拆分 https://www.luogu.org/problemnew/show/P1117 后缀数组我忘了. 此题哈希可解决95分(= =) 设\(l_i ...
- STM8S——Flash program memory and data EEPROM
1.简介 STM8S内部的FLASH程序存储器和数据EEPROM是由一组通用寄存器来控制的:所以我们可以通过这些通用寄存器来编程或擦除存储器的内容.设置写保护.或者配置特定的低功耗模式.我们也可以自己 ...