zoukankan      html  css  js  c++  java
  • [leetcode]Add Bold Tag in String

    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
    

      

  • 相关阅读:
    flex产生水平滚动条
    js中的类
    typescript
    vue练习
    vue-cli2脚手架搭建
    Luogu P1970 花匠
    Luogu P1311 选择客栈
    Luogu P1016 旅行家的预算
    Luogu P1144 最短路计数
    Luogu P1091 合唱队形
  • 原文地址:https://www.cnblogs.com/lautsie/p/12245715.html
Copyright © 2011-2022 走看看