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

凡科网站建站后 怎么编辑自己的代码源怎么给网站加速

凡科网站建站后 怎么编辑自己的代码源,怎么给网站加速,茶楼 网站,南宁画册设计公司1410. HTML 实体解析器 1410. HTML 实体解析器 代码仓库地址: https://github.com/slience-me/Leetcode 个人博客 :https://slienceme.xyz 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""…

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.yayakq.cn/news/896822/

相关文章:

  • 网站建设落后发言广西云尚网络科技有限公司
  • 佛山做网站建设价格wordpress注册默认密码
  • 天津建设工程信息网专家网站运行速度优化
  • 网站建设有什么岗位天元建设集团有限公司企业简介
  • 新乡网站建设哪家好服务器类网站建设
  • php网站做分享到朋友圈怎样免费创建网站
  • 企业网站创建小结关于我们网页设计模板
  • 郑州建设网站有哪些装修设计公司网站有哪些
  • 关于建设集团公司网站的报告电子商务网站建设 李洪心
  • 宽屏企业网站模板个人网站可以做app吗
  • html网站素材网搜索网站老是跳出别的网站要怎么做
  • 深圳常平网站建设制作公司网站做负载均衡
  • 机关作风建设网站网站 商城 app 建设
  • wap网站代码内江 网站建设
  • 金币交易网站开发wordpress默认原始图片
  • 自己做网站的软件厦门做网站哪家公司好
  • 关于怎么做网站广州营销网站制作
  • 网站开发心得都匀网站制作
  • 电商平台介绍网站模板网站视频要vip怎么看
  • 百度统计网站速度诊断网页设计与制作学什么
  • 微信游戏网站开发河南省汝州文明建设门户网站
  • vps网站管理软件注册公司咨询
  • 一个空间可以放两个网站吗网站建设与程序编辑
  • 怎样取消网站备案wordpress 文章标题调用
  • google浏览器官网下载网站优化连云港哪家强?
  • 用asp怎么做网站更改wordpress语言设置
  • 广州网站运营十年乐云seo鄂尔多斯seo
  • 如何让百度新闻收录网站文章学做网站的书哪些好
  • 做汽车销售要了解的网站营销型网站建设方法
  • 如何破解网站后台账号和密码太原网络营销