1.(字符串)-子字符串位置查找

如:给定acdbcdbdse,查询:dbd。返回5

Python:

class Solution:
    def strStr(self, source, target):
        if source is None or target is None:
            return -1

        for i in range(len(source) - len(target) + 1):
            for j in range(len(target)):
                if source[i + j] != target[j]:
                    break
            else:  #python:for else;for内全部正常执行完毕(不执行break)时,执行else
                return i
        return -1

s=Solution()
a=s.strStr('acdbcdbdse','dbd')
print(a)

 

posted @ 2019-02-12 19:29  jj千寻  阅读(120)  评论(0编辑  收藏  举报