strStr
来源:互联网 发布:乐股软件免费下载 编辑:程序博客网 时间:2024/05/20 21:20
题目链接:http://www.lintcode.com/zh-cn/problem/strstr/
题目描述:
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。
样例:
如果 source = "source" 和 target = "target",返回 -1。
如果 source = "abcdabcdefg" 和 target = "bcd",返回 1。
这道题首先要判断非空,因为如果为空直接调用的话会报错。
class Solution: def strStr(self, source, target): if source is None or target is None: # 这里首先要判断source和target是否为空,如果为空的话就直接返回-1 return -1 return source.find(target) # 如果非空,直接调用标准库中的函数即可。
0 0
- strstr
- strstr
- strstr
- strstr()
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strStr
- strstr
- This app has been built with an incorrect configuration. Please configure your build for VectorDrawa
- HTML 18.3 表单元素input的输入属性type
- masterJ2EE篇001——启动tomcat报错Server Tomcat v7.0 Server at localhost failed to start
- 高效MacBook工作环境配置
- Caffe代码解读(四):solver_param
- strStr
- last-position-of-target
- search-insert-position
- search-a-2d-matrix
- first-position-of-target
- search-in-a-big-sorted-array
- wood-cut
- find-minimum-in-rotated-sorted-array
- find-peak-element