Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bf2aab

Browse files
feat: add solutions to lc problem: No.1410 (#2002)
No.1410.HTML Entity Parser
1 parent 85034d7 commit 0bf2aab

File tree

4 files changed

+260
-57
lines changed

4 files changed

+260
-57
lines changed

‎solution/1400-1499/1410.HTML Entity Parser/README.md‎

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75-
**方法一:哈希表**
75+
**方法一:哈希表 + 模拟**
76+
77+
我们可以使用哈希表来存储每个字符实体对应的字符,然后遍历字符串,当遇到字符实体时,我们就将其替换为对应的字符。
78+
79+
时间复杂度 $O(n \times l),ドル空间复杂度 $O(l)$。其中 $n$ 是字符串的长度,而 $l$ 是字符实体的总长度。
7680

7781
<!-- tabs:start -->
7882

@@ -124,20 +128,20 @@ class Solution {
124128
int i = 0;
125129
int n = text.length();
126130
while (i < n) {
127-
boolean find = false;
131+
boolean found = false;
128132
for (int l = 1; l < 8; ++l) {
129133
int j = i + l;
130134
if (j <= n) {
131135
String t = text.substring(i, j);
132136
if (d.containsKey(t)) {
133137
ans.append(d.get(t));
134138
i = j;
135-
find = true;
139+
found = true;
136140
break;
137141
}
138142
}
139143
}
140-
if (!find) {
144+
if (!found) {
141145
ans.append(text.charAt(i++));
142146
}
143147
}
@@ -152,38 +156,117 @@ class Solution {
152156
class Solution {
153157
public:
154158
string entityParser(string text) {
155-
unordered_map<string, string> d;
156-
d["&quot;"] = "\"";
157-
d["&apos;"] = "'";
158-
d["&amp;"] = "&";
159-
d["&gt;"] = ">";
160-
d["&lt;"] = "<";
161-
d["&frasl;"] = "/";
159+
unordered_map<string, string> d = {
160+
{"&quot;", "\""},
161+
{"&apos;", "'"},
162+
{"&amp;", "&"},
163+
{"&gt;", ">"},
164+
{"&lt;", "<"},
165+
{"&frasl;", "/"},
166+
};
162167
string ans = "";
163168
int i = 0, n = text.size();
164169
while (i < n) {
165-
bool find = false;
170+
bool found = false;
166171
for (int l = 1; l < 8; ++l) {
167172
int j = i + l;
168173
if (j <= n) {
169174
string t = text.substr(i, l);
170175
if (d.count(t)) {
171176
ans += d[t];
172177
i = j;
173-
find = true;
178+
found = true;
174179
break;
175180
}
176181
}
177182
}
178-
if (!find) ans += text[i++];
183+
if (!found) ans += text[i++];
179184
}
180185
return ans;
181186
}
182187
};
183188
```
184189
190+
### **Go**
191+
192+
```go
193+
func entityParser(text string) string {
194+
d := map[string]string{
195+
"&quot;": "\"",
196+
"&apos;": "'",
197+
"&amp;": "&",
198+
"&gt;": ">",
199+
"&lt;": "<",
200+
"&frasl;": "/",
201+
}
202+
var ans strings.Builder
203+
i, n := 0, len(text)
204+
205+
for i < n {
206+
found := false
207+
for l := 1; l < 8; l++ {
208+
j := i + l
209+
if j <= n {
210+
t := text[i:j]
211+
if val, ok := d[t]; ok {
212+
ans.WriteString(val)
213+
i = j
214+
found = true
215+
break
216+
}
217+
}
218+
}
219+
if !found {
220+
ans.WriteByte(text[i])
221+
i++
222+
}
223+
}
224+
225+
return ans.String()
226+
}
227+
```
228+
185229
### **TypeScript**
186230

231+
```ts
232+
function entityParser(text: string): string {
233+
const d: Record<string, string> = {
234+
'&quot;': '"',
235+
'&apos;': "'",
236+
'&amp;': '&',
237+
'&gt;': '>',
238+
'&lt;': '<',
239+
'&frasl;': '/',
240+
};
241+
242+
let ans: string = '';
243+
let i: number = 0;
244+
const n: number = text.length;
245+
246+
while (i < n) {
247+
let found: boolean = false;
248+
for (let l: number = 1; l < 8; ++l) {
249+
const j: number = i + l;
250+
if (j <= n) {
251+
const t: string = text.substring(i, j);
252+
if (d.hasOwnProperty(t)) {
253+
ans += d[t];
254+
i = j;
255+
found = true;
256+
break;
257+
}
258+
}
259+
}
260+
261+
if (!found) {
262+
ans += text[i++];
263+
}
264+
}
265+
266+
return ans;
267+
}
268+
```
269+
187270
```ts
188271
function entityParser(text: string): string {
189272
const d: { [key: string]: string } = {

‎solution/1400-1499/1410.HTML Entity Parser/README_EN.md‎

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Hash Table + Simulation**
51+
52+
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.
53+
54+
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.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
@@ -93,20 +99,20 @@ class Solution {
9399
int i = 0;
94100
int n = text.length();
95101
while (i < n) {
96-
boolean find = false;
102+
boolean found = false;
97103
for (int l = 1; l < 8; ++l) {
98104
int j = i + l;
99105
if (j <= n) {
100106
String t = text.substring(i, j);
101107
if (d.containsKey(t)) {
102108
ans.append(d.get(t));
103109
i = j;
104-
find = true;
110+
found = true;
105111
break;
106112
}
107113
}
108114
}
109-
if (!find) {
115+
if (!found) {
110116
ans.append(text.charAt(i++));
111117
}
112118
}
@@ -121,38 +127,117 @@ class Solution {
121127
class Solution {
122128
public:
123129
string entityParser(string text) {
124-
unordered_map<string, string> d;
125-
d["&quot;"] = "\"";
126-
d["&apos;"] = "'";
127-
d["&amp;"] = "&";
128-
d["&gt;"] = ">";
129-
d["&lt;"] = "<";
130-
d["&frasl;"] = "/";
130+
unordered_map<string, string> d = {
131+
{"&quot;", "\""},
132+
{"&apos;", "'"},
133+
{"&amp;", "&"},
134+
{"&gt;", ">"},
135+
{"&lt;", "<"},
136+
{"&frasl;", "/"},
137+
};
131138
string ans = "";
132139
int i = 0, n = text.size();
133140
while (i < n) {
134-
bool find = false;
141+
bool found = false;
135142
for (int l = 1; l < 8; ++l) {
136143
int j = i + l;
137144
if (j <= n) {
138145
string t = text.substr(i, l);
139146
if (d.count(t)) {
140147
ans += d[t];
141148
i = j;
142-
find = true;
149+
found = true;
143150
break;
144151
}
145152
}
146153
}
147-
if (!find) ans += text[i++];
154+
if (!found) ans += text[i++];
148155
}
149156
return ans;
150157
}
151158
};
152159
```
153160
161+
### **Go**
162+
163+
```go
164+
func entityParser(text string) string {
165+
d := map[string]string{
166+
"&quot;": "\"",
167+
"&apos;": "'",
168+
"&amp;": "&",
169+
"&gt;": ">",
170+
"&lt;": "<",
171+
"&frasl;": "/",
172+
}
173+
var ans strings.Builder
174+
i, n := 0, len(text)
175+
176+
for i < n {
177+
found := false
178+
for l := 1; l < 8; l++ {
179+
j := i + l
180+
if j <= n {
181+
t := text[i:j]
182+
if val, ok := d[t]; ok {
183+
ans.WriteString(val)
184+
i = j
185+
found = true
186+
break
187+
}
188+
}
189+
}
190+
if !found {
191+
ans.WriteByte(text[i])
192+
i++
193+
}
194+
}
195+
196+
return ans.String()
197+
}
198+
```
199+
154200
### **TypeScript**
155201

202+
```ts
203+
function entityParser(text: string): string {
204+
const d: Record<string, string> = {
205+
'&quot;': '"',
206+
'&apos;': "'",
207+
'&amp;': '&',
208+
'&gt;': '>',
209+
'&lt;': '<',
210+
'&frasl;': '/',
211+
};
212+
213+
let ans: string = '';
214+
let i: number = 0;
215+
const n: number = text.length;
216+
217+
while (i < n) {
218+
let found: boolean = false;
219+
for (let l: number = 1; l < 8; ++l) {
220+
const j: number = i + l;
221+
if (j <= n) {
222+
const t: string = text.substring(i, j);
223+
if (d.hasOwnProperty(t)) {
224+
ans += d[t];
225+
i = j;
226+
found = true;
227+
break;
228+
}
229+
}
230+
}
231+
232+
if (!found) {
233+
ans += text[i++];
234+
}
235+
}
236+
237+
return ans;
238+
}
239+
```
240+
156241
```ts
157242
function entityParser(text: string): string {
158243
const d: { [key: string]: string } = {
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
class Solution {
2-
public:
3-
string entityParser(string text) {
4-
unordered_map<string, string> d;
5-
d["&quot;"] = "\"";
6-
d["&apos;"] = "'";
7-
d["&amp;"] = "&";
8-
d["&gt;"] = ">";
9-
d["&lt;"] = "<";
10-
d["&frasl;"] = "/";
11-
string ans = "";
12-
int i = 0, n = text.size();
13-
while (i < n) {
14-
bool find = false;
15-
for (int l = 1; l < 8; ++l) {
16-
int j = i + l;
17-
if (j <= n) {
18-
string t = text.substr(i, l);
19-
if (d.count(t)) {
20-
ans += d[t];
21-
i = j;
22-
find = true;
23-
break;
24-
}
25-
}
26-
}
27-
if (!find) ans += text[i++];
28-
}
29-
return ans;
30-
}
1+
class Solution {
2+
public:
3+
string entityParser(string text) {
4+
unordered_map<string, string> d = {
5+
{"&quot;", "\""},
6+
{"&apos;", "'"},
7+
{"&amp;", "&"},
8+
{"&gt;", ">"},
9+
{"&lt;", "<"},
10+
{"&frasl;", "/"},
11+
};
12+
string ans = "";
13+
int i = 0, n = text.size();
14+
while (i < n) {
15+
bool found = false;
16+
for (int l = 1; l < 8; ++l) {
17+
int j = i + l;
18+
if (j <= n) {
19+
string t = text.substr(i, l);
20+
if (d.count(t)) {
21+
ans += d[t];
22+
i = j;
23+
found = true;
24+
break;
25+
}
26+
}
27+
}
28+
if (!found) ans += text[i++];
29+
}
30+
return ans;
31+
}
3132
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /