diff --git a/solution/1400-1499/1410.HTML Entity Parser/README.md b/solution/1400-1499/1410.HTML Entity Parser/README.md index ea713290133ae..4ac7905a81fc0 100644 --- a/solution/1400-1499/1410.HTML Entity Parser/README.md +++ b/solution/1400-1499/1410.HTML Entity Parser/README.md @@ -72,7 +72,11 @@ -**方法一:哈希表** +**方法一:哈希表 + 模拟** + +我们可以使用哈希表来存储每个字符实体对应的字符,然后遍历字符串,当遇到字符实体时,我们就将其替换为对应的字符。 + +时间复杂度 $O(n \times l)$,空间复杂度 $O(l)$。其中 $n$ 是字符串的长度,而 $l$ 是字符实体的总长度。 @@ -124,7 +128,7 @@ class Solution { int i = 0; int n = text.length(); while (i < n) { - boolean find = false; + boolean found = false; for (int l = 1; l < 8; ++l) { int j = i + l; if (j <= n) { @@ -132,12 +136,12 @@ class Solution { if (d.containsKey(t)) { ans.append(d.get(t)); i = j; - find = true; + found = true; break; } } } - if (!find) { + if (!found) { ans.append(text.charAt(i++)); } } @@ -152,17 +156,18 @@ class Solution { class Solution { public: string entityParser(string text) { - unordered_map d; - d["""] = "\""; - d["'"] = "'"; - d["&"] = "&"; - d[">"] = ">"; - d["<"] = "<"; - d["⁄"] = "/"; + unordered_map d = { + {""", "\""}, + {"'", "'"}, + {"&", "&"}, + {">", ">"}, + {"<", "<"}, + {"⁄", "/"}, + }; string ans = ""; int i = 0, n = text.size(); while (i < n) { - bool find = false; + bool found = false; for (int l = 1; l < 8; ++l) { int j = i + l; if (j <= n) { @@ -170,20 +175,98 @@ public: if (d.count(t)) { ans += d[t]; i = j; - find = true; + found = true; break; } } } - if (!find) ans += text[i++]; + if (!found) ans += text[i++]; } return ans; } }; ``` +### **Go** + +```go +func entityParser(text string) string { + d := map[string]string{ + """: "\"", + "'": "'", + "&": "&", + ">": ">", + "<": "<", + "⁄": "/", + } + var ans strings.Builder + i, n := 0, len(text) + + for i < n { + found := false + for l := 1; l < 8; l++ { + j := i + l + if j <= n { + t := text[i:j] + if val, ok := d[t]; ok { + ans.WriteString(val) + i = j + found = true + break + } + } + } + if !found { + ans.WriteByte(text[i]) + i++ + } + } + + return ans.String() +} +``` + ### **TypeScript** +```ts +function entityParser(text: string): string { + const d: Record = { + '"': '"', + ''': "'", + '&': '&', + '>': '>', + '<': '<', + '⁄': '/', + }; + + let ans: string = ''; + let i: number = 0; + const n: number = text.length; + + while (i < n) { + let found: boolean = false; + for (let l: number = 1; l < 8; ++l) { + const j: number = i + l; + if (j <= n) { + const t: string = text.substring(i, j); + if (d.hasOwnProperty(t)) { + ans += d[t]; + i = j; + found = true; + break; + } + } + } + + if (!found) { + ans += text[i++]; + } + } + + return ans; +} +``` + ```ts function entityParser(text: string): string { const d: { [key: string]: string } = { diff --git a/solution/1400-1499/1410.HTML Entity Parser/README_EN.md b/solution/1400-1499/1410.HTML Entity Parser/README_EN.md index 2f05d08cce4ab..019d7d2612196 100644 --- a/solution/1400-1499/1410.HTML Entity Parser/README_EN.md +++ b/solution/1400-1499/1410.HTML Entity Parser/README_EN.md @@ -47,6 +47,12 @@ ## Solutions +**Solution 1: Hash Table + Simulation** + +We can use a hash table to store the corresponding character for each character entity. Then, we traverse the string, and when we encounter a character entity, we replace it with the corresponding character. + +The time complexity is $O(n \times l)$, and the space complexity is $O(l)$. Here, $n$ is the length of the string, and $l$ is the total length of the character entities. + ### **Python3** @@ -93,7 +99,7 @@ class Solution { int i = 0; int n = text.length(); while (i < n) { - boolean find = false; + boolean found = false; for (int l = 1; l < 8; ++l) { int j = i + l; if (j <= n) { @@ -101,12 +107,12 @@ class Solution { if (d.containsKey(t)) { ans.append(d.get(t)); i = j; - find = true; + found = true; break; } } } - if (!find) { + if (!found) { ans.append(text.charAt(i++)); } } @@ -121,17 +127,18 @@ class Solution { class Solution { public: string entityParser(string text) { - unordered_map d; - d["""] = "\""; - d["'"] = "'"; - d["&"] = "&"; - d[">"] = ">"; - d["<"] = "<"; - d["⁄"] = "/"; + unordered_map d = { + {""", "\""}, + {"'", "'"}, + {"&", "&"}, + {">", ">"}, + {"<", "<"}, + {"⁄", "/"}, + }; string ans = ""; int i = 0, n = text.size(); while (i < n) { - bool find = false; + bool found = false; for (int l = 1; l < 8; ++l) { int j = i + l; if (j <= n) { @@ -139,20 +146,98 @@ public: if (d.count(t)) { ans += d[t]; i = j; - find = true; + found = true; break; } } } - if (!find) ans += text[i++]; + if (!found) ans += text[i++]; } return ans; } }; ``` +### **Go** + +```go +func entityParser(text string) string { + d := map[string]string{ + """: "\"", + "'": "'", + "&": "&", + ">": ">", + "<": "<", + "⁄": "/", + } + var ans strings.Builder + i, n := 0, len(text) + + for i < n { + found := false + for l := 1; l < 8; l++ { + j := i + l + if j <= n { + t := text[i:j] + if val, ok := d[t]; ok { + ans.WriteString(val) + i = j + found = true + break + } + } + } + if !found { + ans.WriteByte(text[i]) + i++ + } + } + + return ans.String() +} +``` + ### **TypeScript** +```ts +function entityParser(text: string): string { + const d: Record = { + '"': '"', + ''': "'", + '&': '&', + '>': '>', + '<': '<', + '⁄': '/', + }; + + let ans: string = ''; + let i: number = 0; + const n: number = text.length; + + while (i < n) { + let found: boolean = false; + for (let l: number = 1; l < 8; ++l) { + const j: number = i + l; + if (j <= n) { + const t: string = text.substring(i, j); + if (d.hasOwnProperty(t)) { + ans += d[t]; + i = j; + found = true; + break; + } + } + } + + if (!found) { + ans += text[i++]; + } + } + + return ans; +} +``` + ```ts function entityParser(text: string): string { const d: { [key: string]: string } = { diff --git a/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp b/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp index 280a6ee64a2c6..6bcfda15178fb 100644 --- a/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp +++ b/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp @@ -1,31 +1,32 @@ -class Solution { -public: - string entityParser(string text) { - unordered_map d; - d["""] = "\""; - d["'"] = "'"; - d["&"] = "&"; - d[">"] = ">"; - d["<"] = "<"; - d["⁄"] = "/"; - string ans = ""; - int i = 0, n = text.size(); - while (i < n) { - bool find = false; - for (int l = 1; l < 8; ++l) { - int j = i + l; - if (j <= n) { - string t = text.substr(i, l); - if (d.count(t)) { - ans += d[t]; - i = j; - find = true; - break; - } - } - } - if (!find) ans += text[i++]; - } - return ans; - } +class Solution { +public: + string entityParser(string text) { + unordered_map d = { + {""", "\""}, + {"'", "'"}, + {"&", "&"}, + {">", ">"}, + {"<", "<"}, + {"⁄", "/"}, + }; + string ans = ""; + int i = 0, n = text.size(); + while (i < n) { + bool found = false; + for (int l = 1; l < 8; ++l) { + int j = i + l; + if (j <= n) { + string t = text.substr(i, l); + if (d.count(t)) { + ans += d[t]; + i = j; + found = true; + break; + } + } + } + if (!found) ans += text[i++]; + } + return ans; + } }; \ No newline at end of file diff --git a/solution/1400-1499/1410.HTML Entity Parser/Solution.go b/solution/1400-1499/1410.HTML Entity Parser/Solution.go new file mode 100644 index 0000000000000..ed3b22c1bf382 --- /dev/null +++ b/solution/1400-1499/1410.HTML Entity Parser/Solution.go @@ -0,0 +1,34 @@ +func entityParser(text string) string { + d := map[string]string{ + """: "\"", + "'": "'", + "&": "&", + ">": ">", + "<": "<", + "⁄": "/", + } + var ans strings.Builder + i, n := 0, len(text) + + for i < n { + found := false + for l := 1; l < 8; l++ { + j := i + l + if j <= n { + t := text[i:j] + if val, ok := d[t]; ok { + ans.WriteString(val) + i = j + found = true + break + } + } + } + if !found { + ans.WriteByte(text[i]) + i++ + } + } + + return ans.String() +} \ No newline at end of file