Implement strStr()
发布日期:2021-05-13 00:12:57 浏览次数:17 分类:精选文章

本文共 749 字,大约阅读时间需要 2 分钟。

For a given source string and a target string, you should output the first index(from 0) of target string in source string.If target does not exist in source, just return -1.
class Solution:    """    @param source:     @param target:     @return: return the index    """    def getnext(self, target):        k = -1        j = 1        nexts = [-1 for i in range(len(target))]        while j < len(target) - 1:            if k == -1 or target[j] == target[k]:                 j += 1                k += 1                nexts[j] = k            else:                k = nexts[k]        return nexts            def strStr(self, source, target):        # Write your code here        nexts = self.getnext(target)        i = 0        j = 0        while i
上一篇:Merge k sorted linked lists
下一篇:14. First Position of Target

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月11日 14时21分56秒