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 ...
随机推荐
- java常用API之Date类
Date类: 类 Date 表示特定的瞬间,精确到毫秒. 毫秒概念:1000毫秒=1秒 毫秒的0点: System.currentTimeMillis() 返回值long类型参数 用于获取当前日 ...
- js遍历添加栏目类添加css 再点击其它删除css
//js遍历添加栏目类添加css 再点击其它删除css $(".radio-group .ckselect").each(function(index) { $(this).cli ...
- vue父组件为子组件传值传不过去?vue为数组传值,不能直接用等于的方式,要用循环加push的方式
父组件为子组件传值不成功,子组件拿不到值,不能直接赋值,要用循环加push的方式赋值.
- R语言学习笔记(十九):字符串处理中预定义字符组(表格介绍)
R中预定义的字符组 代码 含义说明 [:digit:]或\\d 数字; [0-9] [^[:digit:]]或\\D 非数字; 等价于[^0-9] [:lower:] 小写字母; [a-z] [:up ...
- 针对铁定浏览器的css选择符
/***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...
- 《Java 程序设计》实验报告汇总
<Java 程序设计>实验报告汇总 20145207<Java程序设计>实验一 (Java开发环境的熟悉)实验报告 20145207<Java程序设计>实验二 (J ...
- mfc 类的友元函数
知识点 友元函数 友元函数 友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数..类授予它的友元特别的访问权.通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你 ...
- 【转载】基于MFC的ActiveX控件开发(3)
原文:http://iysm.net/?p=122 3.事件 ActiveX 控件使用事件通知容器控件上发生了某些事情.事件的常见示例包括单击控件.使用键盘输入数据和控件状态更改.当发生这些操作时,控 ...
- 4710: [Jsoi2011]分特产
4710: [Jsoi2011]分特产 链接 分析: 容斥原理+隔板法. 代码: #include<cstdio> #include<algorithm> #include&l ...
- TPO-22 C2 Revise a music history paper
第 1 段 1.Listen to part of a conversation between a student and his music history professor. :听一段学生和音 ...