https://github.com/September26/java-algorithms
「HTML 实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。
HTML 里这些特殊字符和它们对应的字符实体包括:
"
,对应的字符是 "
。'
,对应的字符是 '
。&
,对应对的字符是 &
。>
,对应的字符是 >
。<
,对应的字符是 <
。⁄
,对应的字符是 /
。给你输入字符串 text
,请你实现一个 HTML 实体解析器,返回解析器解析后的结果。
示例 1:
输入:text = "& is an HTML entity but &ambassador; is not." 输出:"& is an HTML entity but &ambassador; is not." 解释:解析器把字符实体 & 用 & 替换
示例 2:
输入:text = "and I quote: "..."" 输出:"and I quote: \"...\""
示例 3:
输入:text = "Stay home! Practice on Leetcode :)" 输出:"Stay home! Practice on Leetcode :)"
示例 4:
输入:text = "x > y && x < y is always false" 输出:"x > y && x < y is always false"
示例 5:
输入:text = "leetcode.com⁄problemset⁄all" 输出:"leetcode.com/problemset/all"
提示:
1 <= text.length <= 10^5
遍历字符串中的每一个字符,如果字符串及其后面的字符可匹配,则index+=匹配的长度。
否则index++即可。
class Solution {
public:
vector v1 = {""", "'", "&", ">", "<", "⁄"};
vector v2 = {"\"", "\'", "&", ">", "<", "/"};
pair isMatchReplace(string &text, int index)
{
for (int i = 0; i < v1.size(); i++)
{
if (text.compare(index, v1[i].size(), v1[i]) == 0)
{
int k = v1[i].size();
return make_pair(static_cast(v2[i]), v1[i].size());
}
}
return make_pair(text.substr(index, 1), 1);
}
string entityParser(string text)
{
int index = 0;
ostringstream out;
pair pair;
while (index < text.size())
{
pair = isMatchReplace(text, index);
out << pair.first;
index += pair.second;
}
return out.str();
}
};