【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:暴力解

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

cpp 复制代码
class Solution {
public:
    string entityParser(string text) {
        unordered_map<string, string> myMap = {{"&quot;",  "\""},
                                             {"'",  "\'"},
                                             {"&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

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

cpp 复制代码
class Solution {
public:
    string entityParser(string text) {
        unordered_map<string, string> myMap = {{"&quot;",  "\""},
                                               {"'",  "\'"},
                                               {"&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

最后的优化

cpp 复制代码
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) == "'") {
                    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++ 的用户

相关推荐
chao_78933 分钟前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
我在北京coding1 小时前
6套bootstrap后台管理界面源码
前端·bootstrap·html
曦月逸霜1 小时前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
海的诗篇_3 小时前
移除元素-JavaScript【算法学习day.04】
javascript·学习·算法
自动驾驶小卡3 小时前
A*算法实现原理以及实现步骤(C++)
算法
Unpredictable2223 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
编程绿豆侠3 小时前
力扣HOT100之多维动态规划:1143. 最长公共子序列
算法·leetcode·动态规划
珂朵莉MM3 小时前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·职场和发展·机器人
fail_to_code4 小时前
递归法的递归函数何时需要返回值
算法