当前位置: 首页 > news >正文

【Leetcode合集】1410. HTML 实体解析器

1410. HTML 实体解析器

1410. HTML 实体解析器

代码仓库地址: https://github.com/slience-me/Leetcode

个人博客 :https://slienceme.xyz

  • 编写一个函数来查找字符串数组中的最长公共前缀。

    如果不存在公共前缀,返回空字符串 ""

  • 「HTML 实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。

    HTML 里这些特殊字符和它们对应的字符实体包括:

    • 双引号: 字符实体为 " ,对应的字符是 "
    • 单引号: 字符实体为 ' ,对应的字符是 '
    • 与符号: 字符实体为 & ,对应对的字符是 &
    • 大于号: 字符实体为 > ,对应的字符是 >
    • 小于号: 字符实体为 < ,对应的字符是 <
    • 斜线号: 字符实体为 ,对应的字符是 /

    给你输入字符串 text ,请你实现一个 HTML 实体解析器,返回解析器解析后的结果。

    示例 1:

    输入:text = "&amp; is an HTML entity but &ambassador; is not."
    输出:"& is an HTML entity but &ambassador; is not."
    解释:解析器把字符实体 &amp; 用 & 替换
    

    示例 2:

    输入:text = "and I quote: &quot;...&quot;"
    输出:"and I quote: \"...\""
    

    示例 3:

    输入:text = "Stay home! Practice on Leetcode :)"
    输出:"Stay home! Practice on Leetcode :)"
    

    示例 4:

    输入:text = "x &gt; y &amp;&amp; x &lt; y is always false"
    输出:"x > y && x < y is always false"
    

    示例 5:

    输入:text = "leetcode.com&frasl;problemset&frasl;all"
    输出:"leetcode.com/problemset/all"
    

    提示:

    • 1 <= text.length <= 10^5
    • 字符串可能包含 256 个ASCII 字符中的任意字符。

方案1:暴力解

第一种纯暴力解,遍历替换

class Solution {
public:string entityParser(string text) {unordered_map<string, string> myMap = {{"&quot;",  "\""},{"&apos;",  "\'"},{"&amp;",   "&"},{"&gt;",    ">"},{"&lt;",    "<"},{"&frasl;", "/"}};for (const auto &map: myMap){string searchString = map.first;string replacementString = map.second;size_t pos = text.find(searchString); // 找到第一个匹配的位置// 循环替换所有匹配的内容while (pos != string::npos) {text.replace(pos, searchString.length(), replacementString); // 用替换字符串替换匹配字符串pos = text.find(searchString, pos + replacementString.length()); // 继续找下一个匹配的位置}}return text;}
};

执行用时分布 744ms 击败11.76%使用 C++ 的用户

消耗内存分布16.37MB 击败90.20%使用 C++ 的用户

方案2

发现没有优化太多,反而超时了

class Solution {
public:string entityParser(string text) {unordered_map<string, string> myMap = {{"&quot;",  "\""},{"&apos;",  "\'"},{"&amp;",   "&"},{"&gt;",    ">"},{"&lt;",    "<"},{"&frasl;", "/"}};for (int i = 0; i < text.length(); ++i){string temp="&";if (text[i]=='&'){if (text[i+1]=='\0' || text[i+1]=='&'){continue;}int indexj = i+1;while (text[indexj]!=';'){if (indexj>=text.length()){break;}temp += text[indexj];indexj+=1;}if (indexj>=text.length()){break;}temp += ';';size_t index = replaceStr(text, myMap, temp);if (index != -1){i = index;}} else if(text[i]=='\0'){continue;}}return text;}size_t replaceStr(string &text, unordered_map<string, string> &myMap, const string &temp) const {if(myMap.find(temp) != myMap.end()){string searchString = myMap.find(temp)->first;string replacementString = myMap.find(temp)->second;size_t pos = text.find(searchString); // 找到第一个匹配的位置// 循环替换所有匹配的内容text.replace(pos, searchString.length(), replacementString); // 用替换字符串替换匹配字符串return pos+replacementString.length()-1;}return -1;}
};

超出时间限制

测试用例通过了,但耗时太长。

方案3

最后的优化

class Solution {
public:string entityParser(string text) {string result = "";int i = 0;int n = text.length();while (i < n) {if (text[i] == '&') {if (text.substr(i, 6) == "&quot;") {result += "\"";i += 6;} else if (text.substr(i, 6) == "&apos;") {result += "'";i += 6;} else if (text.substr(i, 5) == "&amp;") {result += "&";i += 5;} else if (text.substr(i, 4) == "&gt;") {result += ">";i += 4;} else if (text.substr(i, 4) == "&lt;") {result += "<";i += 4;} else if (text.substr(i, 7) == "&frasl;") {result += "/";i += 7;} else {result += text[i];i++;}} else {result += text[i];i++;}}return result;}
};

执行用时分布 68ms 击败80.39%使用 C++ 的用户

消耗内存分布 18.54MB 击败35.29%使用 C++ 的用户

http://www.lryc.cn/news/243018.html

相关文章:

  • 04-React脚手架 集成Axios
  • 时序预测 | MATLAB实现基于BiLSTM-AdaBoost双向长短期记忆网络结合AdaBoost时间序列预测
  • 【nlp】3.6 Tansformer模型构建(编码器与解码器模块耦合)
  • 【【Linux系统下常用指令学习 之 二 】】
  • Git-将指定文件回退到指定版本
  • docker环境安装
  • 【Java】智慧工地云平台源码(APP+SaaS模式)
  • 2016年11月10日 Go生态洞察:七年的Go语言旅程
  • 深入了解Java中SQL优化的关键技巧与实践
  • 6.3.WebRTC中的SDP类的结构
  • ArcGis如何用点连线?
  • 自定义精美商品分类列表组件 侧边栏商品分类组件 category组件(适配vue3)
  • 造一个float类型二维矩阵,并将二维矩阵存快速储到一个float*中(memcpy)
  • python通过继承、组合、委托组织类
  • OSG粒子系统与阴影-自定义粒子系统示例<1>(4)
  • 激活函数与其导数:神经网络中的关键元素
  • 微信公众号对接获取用户openid预约项目心路全历程
  • 大中小协作 共筑科学梦——华中科技大学附属花城中学举办首届科技节
  • ElasticSearch之Health API
  • 图的建立基本操作
  • 影响语音芯片识别率的因素概述
  • 操作系统的主要功能--处理机、存储器、设备、文件
  • PDF 批量处理软件BatchOutput PDF mac中文版介绍
  • oracle安装的肘腋之疾小合集
  • django(千锋教育)
  • Python 前后端分离项目Vue部署应用
  • Linux中安装MySQ-合集
  • elk 简单操作手册
  • CSS画一条线
  • 分享常用设计模式之单例模式(懒汉模式和饿汉模式)和几种关于设计模式的面试题