首页 > 其他 > 详细

[leetcode]Add Bold Tag in String

时间:2020-01-31 16:56:17      阅读:66      评论:0      收藏:0      [点我收藏+]

python3

小心结尾。另外,zzz为s,zz为substr时,整个字符串都满足,所以要一个一个移动。

答案里用了startswith,更直观

class Solution:
    def addBoldTag(self, s: str, dict: List[str]) -> str:
        mark = [0] * len(s)
        for substr in dict:
            start = 0
            while start + len(substr) <= len(s):
                if s[start:].startswith(substr):
                    for i in range(len(substr)):
                        mark[start + i] = 1
                start += 1
        result = ‘‘
        status = 0
        for i in range(len(mark)):
            if status == 0 and mark[i] == 0:
                result += s[i]
            elif status == 0 and mark[i] == 1:
                result += ‘<b>‘
                result += s[i]
                status = 1
            elif status == 1 and mark[i] == 0:
                result += ‘</b>‘
                result += s[i]
                status = 0
            elif status == 1 and mark[i] == 1:
                result += s[i]
            else:
                print (‘Error‘)
            
        if status == 1:
            result += ‘</b>‘
            status = 0
        return result

  

[leetcode]Add Bold Tag in String

原文:https://www.cnblogs.com/lautsie/p/12245715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!