标签: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("<", "<") text = text.replace("⁄","/") return text
优化:
class Solution: def entityParser(self, text: str) -> str: h = { "&": "&", """: "\"", "'": "\‘", ">": ">", "<": "<", "⁄": "/", } 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)
标签:gif 提交 etc def else closed entity for spl
原文地址:https://www.cnblogs.com/oldby/p/12687175.html