https://leetcode-cn.com/problems/implement-strstr/

# -*- coding:utf-8 -*-

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: stra
        :rtype: int
        """
        return haystack.find(needle)
if __name__ == '__main__':
    haystack = "hello"
    needle = "ll"
    print(Solution().strStr(haystack, needle))

思路:简单题,find即可。如果说非要展开写不用内置函数KMP即可。这里在算法导论动态规划里有提到过。而实际上python内置的find方法就是使用KMP或其改进。

分类: 算法

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注