LeetCode刷题笔记--Python--28. 实现strStr()

class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
# 法一
# n = len(haystack)
# m = len(needle)
# if not m or haystack == needle:
# return 0
# if n < m:
# return -1
# for i in range(n-m+1):
# if haystack[i:i+m] == needle:
# return i
# return -1
# 法二
# if needle in haystack:
# return haystack.index(needle)
# elif not needle or haystack == needle:
# return 0
# else:
# return -1
# 法三
return haystack.find(needle)
以上三个方法,均测试通过。
LeetCode刷题笔记--Python--28. 实现strStr()的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Leetcode刷题笔记(Python 找出所有相加之和为n的k个组合,组合中只允许含有1-9的正整数,并且每种组合中不存在重复的数字。)
eg:输入:k=3,n=9 输出: [[1,2,6],[1,3,5],[2,3,4]] 输入:k=2,n=5 输出:[[1,4][2,3]] #!/usr/bin/env python # -*- c ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
随机推荐
- magelinux(0111)
Web Service 应用层:http, https 实现某类具体应用: 传输层协议:TCP, UDP, SCTP IANA: 0-1023:众所周知,永久地分配给固定的应用使用,特权端口: 102 ...
- 用代码生成UINavigationController 与UITabBarController相结合的简单QQ框架(部分)
首先我们需要搭建一个空的项目,当然xcode6.0以后不支持直接创建空项目,所以我们需要在系统生成项目之后,删除xcode自动给你生成的控制器和storyboard,另外需要在Main Interfa ...
- Shiro会话管理器与验证码实现(十四)
和shiro整合后,使用shiro的session管理,shiro提供sessionDao操作 会话数据. 配置sessionManager
- 【C++】结构体、联合体大小计算
struct结构体大小计算 struct A { char a; int b; char c; } 这个结构体中,char占据1字节,int占据4字节,char占据1字节,而这组数据结构的大小是12字 ...
- .net active up mail 邮件发送
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- C/C++:.hpp与.h区别
原文地址:http://blog.csdn.net/f_zyj/article/details/51735416 .hpp,本质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件, ...
- 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)
关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...
- 001_linux下的log
如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. 以下介绍的 ...
- 关于ajax请求,返回json数据格式
使用servlet测试 后台数据为:返回类型没有设置(方式一) String str = "["+ "{ id:1, pId:0, name:\"可折腾的父节点 ...
- 图解修改Maven本地仓库存储路径
1 从Maven中心仓库下载到本地的jar包的默认存放在"${user.home}/.m2/repository"中,${user.home}表示当前登录系统的用户目录(如&quo ...