Leetcode1410. HTML 实体解析器

Every day a Leetcode

题目来源:1410. HTML 实体解析器

解法1:模拟

遍历字符串 text,每次遇到 '&',就判断以下情况:

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

如果是上述情况,将转换结果插入结果;如果都不是,则直接添加到结果里。

代码:

c 复制代码
/*
 * @lc app=leetcode.cn id=1410 lang=cpp
 *
 * [1410] HTML 实体解析器
 */

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

结果:

复杂度分析:

时间复杂度:O(n),其中 n 是字符串 text 的长度。

空间复杂度:O(1)。

解法2:模拟

本题要求把字符串中所有的「字符实体」替换成对应的字符。

「字符实体」都是由 & 开头的,所以我们只需要遍历一遍字符串,用一个变量 pos\textit{pos}pos 表示当前处理的位置,如果 text[pos]='&',就在这个位置进行探测。假设一个「字符实体」为 e,对应的字符为 c,那么可以通过判断 pos 位置开始,长度和 e 相同的子串是否和 e 相等,如果相等就可以替换。

代码:

c 复制代码
class Solution {
public:
    using EntityChar = pair <string, char>;

    vector <EntityChar> entityList;

    string entityParser(string text) {
        entityList = vector({
            (EntityChar){"&quot;", '"'},
            (EntityChar){"'", '\''},
            (EntityChar){"&amp;", '&'},
            (EntityChar){"&gt;", '>'},
            (EntityChar){"&lt;", '<'},
            (EntityChar){"&frasl;", '/'}
        });

        string r = "";
        for (int pos = 0; pos < text.size(); ) {
            bool isEntity = false;
            if (text[pos] == '&') {
                for (const auto &[e, c]: entityList) {
                    if (text.substr(pos, e.size()) == e) {
                        r.push_back(c);
                        pos += e.size();
                        isEntity = true;
                        break;
                    }
                }
            }
            if (!isEntity) {
                r.push_back(text[pos++]);
                continue;
            }
        }
        return r;
    }
};

结果:

复杂度分析:

时间复杂度:O(k×n),其中 n 是字符串 text 的长度。考虑最坏情况,每个位置都是 &,那么每个位置都要进行 6 次探测,探测的总时间代价和「实体字符」的总长度 k 相关,这里 k=6+6+5+4+4+7=32。

空间复杂度:O(k),这里用了 entityList 作为辅助变量,字符总数为 k+6,故渐进空间复杂度为 O(k+6)=O(k)。

相关推荐
AAA简单玩转程序设计几秒前
C++进阶小技巧:让代码从"能用"变"优雅"
前端·c++
vir0218 分钟前
密码脱落(最长回文子序列)
数据结构·c++·算法
福尔摩斯张31 分钟前
二维数组详解:定义、初始化与实战
linux·开发语言·数据结构·c++·算法·排序算法
大佬,救命!!!37 分钟前
C++函数式策略模式代码练习
开发语言·c++·学习笔记·学习方法·策略模式·迭代加深·多文件编译
冰西瓜60043 分钟前
模与内积(五)矩阵分析与应用 国科大
线性代数·算法·矩阵
BUG创建者1 小时前
项目中使用script-ext-html-webpack-plugin
前端·webpack·html
努力学算法的蒟蒻1 小时前
day17(11.18)——leetcode面试经典150
算法·leetcode·面试
缘友一世1 小时前
模型微调DPO算法原理深入学习和理解
算法·模型微调·dpo
未若君雅裁1 小时前
斐波那契数列 - 动态规划实现 详解笔记
java·数据结构·笔记·算法·动态规划·代理模式
断剑zou天涯1 小时前
【算法笔记】从暴力递归到动态规划(三)
java·算法·动态规划