码迷,mamicode.com
首页 > Web开发 > 详细

leetcode-184周赛-1410-HTML实体解析器

时间:2020-04-12 20:30:37      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:gif   提交   etc   def   else   closed   entity   for   spl   

题目描述:

技术图片

 

 自己的提交:

class Solution:
    def entityParser(self, text: str) -> str:
        text = text.replace(""","\"")
        text = text.replace("'","\‘")
        text = text.replace("&", "&")
        text = text.replace(">",">")
        text = text.replace("&lt;", "<")
        text = text.replace("&frasl;","/")
        return text

优化:

class Solution:
    def entityParser(self, text: str) -> str:
        h = {
            "&amp;": "&",
            "&quot;": "\"",
            "&apos;": "\‘",
            "&gt;": ">",
            "&lt;": "<",
            "&frasl;": "/",
        }
        
        for k, v in h.items():
            text = text.replace(k, v)
        return text

 

另:

技术图片
class Solution(object):
    def entityParser(self, text):
        ans = []
        i = 0
        
        while i < len(text):
            if text[i] != &:
                ans.append(text[i])
                i += 1
                continue
            else:
                if text[i+1:i+6] == quot;:
                    ans.append(")
                    i += 6
                elif text[i+1:i+6] == apos;:
                    ans.append("")
                    i += 6
                elif text[i+1:i+5] == amp;:
                    ans.append(&)
                    i += 5
                elif text[i+1:i+4] == gt;:
                    ans.append(>)
                    i += 4
                elif text[i+1:i+4] == lt;:
                    ans.append(<)
                    i += 4
                elif text[i+1:i+7] == frasl;:
                    ans.append(/)
                    i += 7
                else:
                    ans.append(text[i])
                    i += 1
        
        return "".join(ans)
View Code

 

leetcode-184周赛-1410-HTML实体解析器

标签:gif   提交   etc   def   else   closed   entity   for   spl   

原文地址:https://www.cnblogs.com/oldby/p/12687175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!