[leetcode:python]28.Implement strStr()
来源:互联网 发布:windows 10不能打字 编辑:程序博客网 时间:2024/06/05 03:33
题目:实现strStr()
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题意:
子字符串在字符串中第一次匹配的位置,如果不能匹配返回-1
方法一:性能65ms
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if not needle and not haystack: return 0 if not needle: return 0 if not haystack and needle: return -1 ans = -1 i, hay = 0, len(haystack) k, nee = 0, len(needle) for i in xrange(hay): if haystack[i] == needle[k]: ans = i s = i+1 for k in xrange(1, nee): if s >= hay: return -1 if haystack[s] != needle[k]: ans = -1 k = 0 break s+=1 if ans != -1: return ans return ans
方法二:性能42ms
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ return haystack.find(needle)
使用haystack包的find()函数
0 0
- [leetcode:python]28.Implement strStr()
- Leetcode-Implement strStr()-Python
- 【Leetcode】【python】Implement strStr()
- LeetCode--Implement strStr() Python
- [LeetCode]28.Implement strStr()
- LeetCode --- 28. Implement strStr()
- LeetCode 28.Implement strStr()
- [Leetcode] 28. Implement strStr()
- 【leetcode】28. Implement strStr()
- [leetcode] 28.Implement strStr()
- Leetcode-28.Implement strStr()
- 28. Implement strStr() LeetCode
- leetcode 28. Implement strStr()
- [LeetCode]28. Implement strStr()
- leetcode 28. Implement strStr()
- Leetcode 28. Implement strStr()
- leetcode 28.Implement strStr()
- LeetCode *** 28. Implement strStr()
- 1053. Path of Equal Weight (30)
- javascript -window与document
- 用SparkSubmit.main(args) 提交应用到spark
- ios开发之NSUUID
- linux下使用Bundler + CMVS-PMVS进行三维重建
- [leetcode:python]28.Implement strStr()
- bp算法python实现(bpnn.py)
- 浅谈Vue的单向绑定和双向绑定
- SpringBoot微服务之分布式跟踪系统(springboot+zipkin)
- 【转】利用caffe中自带的工具来可视化loss 和accuracy
- Solr DocValues详解
- centos 杂记
- 反编译
- 分层和页面交互设计知识点