题解 | #最长公共子串#

最长公共子串

http://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac

滑窗这样写可能更加直接一点吧, 1.首先给字符串1定义一个头指针,然后比较str1[left,i]这个子字符串是否在串2中,若在赋值给res 2.若不在,则将left+1,继续向后移动,直到结束

# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# longest common substring
# @param str1 string字符串 the string
# @param str2 string字符串 the string
# @return string字符串
#1AB2345CD
#12345EF
#
class Solution:
    def LCS(self , str1: str, str2: str) -> str:
        res=""
        left=0
        for i in range(len(str1)+1):
            if str1[left:i+1] in str2:
                res=str1[left:i+1]
            else:
                left=left+1
        return res

                    
                        
                
        # write code here
全部评论
这道题放在动态规划下面真的是过分了,始终有三个用例不通过
点赞 回复 分享
发布于 2022-10-26 15:11 陕西

相关推荐

11-13 12:02
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
34
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务