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