做網(wǎng)站界面尺寸是多少網(wǎng)上營銷的平臺有哪些
?目錄鏈接:
力扣編程題-解法匯總_分享+記錄-CSDN博客
GitHub同步刷題項目:
https://github.com/September26/java-algorithms
原題鏈接:力扣(LeetCode)官網(wǎng) - 全球極客摯愛的技術(shù)成長平臺
描述:
「HTML?實體解析器」 是一種特殊的解析器,它將 HTML 代碼作為輸入,并用字符本身替換掉所有這些特殊的字符實體。
HTML 里這些特殊字符和它們對應(yīng)的字符實體包括:
- 雙引號:字符實體為?
"
?,對應(yīng)的字符是?"
?。 - 單引號:字符實體為?
'
?,對應(yīng)的字符是?'
?。 - 與符號:字符實體為?
&
?,對應(yīng)對的字符是?&
?。 - 大于號:字符實體為?
>
?,對應(yīng)的字符是?>
?。 - 小于號:字符實體為?
<
?,對應(yīng)的字符是?<
?。 - 斜線號:字符實體為?
⁄
?,對應(yīng)的字符是?/
?。
給你輸入字符串?text
?,請你實現(xiàn)一個 HTML?實體解析器,返回解析器解析后的結(jié)果。
示例 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
- 字符串可能包含 256 個ASCII 字符中的任意字符。
解題思路:
遍歷字符串中的每一個字符,如果字符串及其后面的字符可匹配,則index+=匹配的長度。
否則index++即可。
代碼:
class Solution {
public:vector<string> v1 = {""", "'", "&", ">", "<", "⁄"};vector<string> v2 = {"\"", "\'", "&", ">", "<", "/"};pair<string, int> 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<string>(v2[i]), v1[i].size());}}return make_pair<string, int>(text.substr(index, 1), 1);}string entityParser(string text){int index = 0;ostringstream out;pair<string, int> pair;while (index < text.size()){pair = isMatchReplace(text, index);out << pair.first;index += pair.second;}return out.str();}
};