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 条评论