Skip to content

Commit 8f24d4f

Browse files
committed
feat: add solutions to lc problem: No.1410
No.1410.HTML Entity Parser
1 parent 625a3e9 commit 8f24d4f

File tree

5 files changed

+283
-2
lines changed

5 files changed

+283
-2
lines changed

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

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,118 @@
7272

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

75+
**方法一:哈希表**
76+
7577
<!-- tabs:start -->
7678

7779
### **Python3**
7880

7981
<!-- 这里可写当前语言的特殊实现逻辑 -->
8082

8183
```python
82-
84+
class Solution:
85+
def entityParser(self, text: str) -> str:
86+
d = {
87+
'&quot;': '"',
88+
'&apos;': "'",
89+
'&amp;': "&",
90+
"&gt;": '>',
91+
"&lt;": '<',
92+
"&frasl;": '/',
93+
}
94+
i, n = 0, len(text)
95+
ans = []
96+
while i < n:
97+
for l in range(1, 8):
98+
j = i + l
99+
if text[i:j] in d:
100+
ans.append(d[text[i:j]])
101+
i = j
102+
break
103+
else:
104+
ans.append(text[i])
105+
i += 1
106+
return ''.join(ans)
83107
```
84108

85109
### **Java**
86110

87111
<!-- 这里可写当前语言的特殊实现逻辑 -->
88112

89113
```java
114+
class Solution {
115+
public String entityParser(String text) {
116+
Map<String, String> d = new HashMap<>();
117+
d.put("&quot;", "\"");
118+
d.put("&apos;", "'");
119+
d.put("&amp;", "&");
120+
d.put("&gt;", ">");
121+
d.put("&lt;", "<");
122+
d.put("&frasl;", "/");
123+
StringBuilder ans = new StringBuilder();
124+
int i = 0;
125+
int n = text.length();
126+
while (i < n) {
127+
boolean find = false;
128+
for (int l = 1; l < 8; ++l) {
129+
int j = i + l;
130+
if (j <= n) {
131+
String t = text.substring(i, j);
132+
if (d.containsKey(t)) {
133+
ans.append(d.get(t));
134+
i = j;
135+
find = true;
136+
break;
137+
}
138+
}
139+
}
140+
if (!find) {
141+
ans.append(text.charAt(i++));
142+
}
143+
}
144+
return ans.toString();
145+
}
146+
}
147+
```
90148

149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
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;"] = "/";
162+
string ans = "";
163+
int i = 0, n = text.size();
164+
while (i < n)
165+
{
166+
bool find = false;
167+
for (int l = 1; l < 8; ++l)
168+
{
169+
int j = i + l;
170+
if (j <= n)
171+
{
172+
string t = text.substr(i, l);
173+
if (d.count(t))
174+
{
175+
ans += d[t];
176+
i = j;
177+
find = true;
178+
break;
179+
}
180+
}
181+
}
182+
if (!find) ans += text[i++];
183+
}
184+
return ans;
185+
}
186+
};
91187
```
92188
93189
### **...**

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

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,107 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def entityParser(self, text: str) -> str:
57+
d = {
58+
'&quot;': '"',
59+
'&apos;': "'",
60+
'&amp;': "&",
61+
"&gt;": '>',
62+
"&lt;": '<',
63+
"&frasl;": '/',
64+
}
65+
i, n = 0, len(text)
66+
ans = []
67+
while i < n:
68+
for l in range(1, 8):
69+
j = i + l
70+
if text[i:j] in d:
71+
ans.append(d[text[i:j]])
72+
i = j
73+
break
74+
else:
75+
ans.append(text[i])
76+
i += 1
77+
return ''.join(ans)
5678
```
5779

5880
### **Java**
5981

6082
```java
83+
class Solution {
84+
public String entityParser(String text) {
85+
Map<String, String> d = new HashMap<>();
86+
d.put("&quot;", "\"");
87+
d.put("&apos;", "'");
88+
d.put("&amp;", "&");
89+
d.put("&gt;", ">");
90+
d.put("&lt;", "<");
91+
d.put("&frasl;", "/");
92+
StringBuilder ans = new StringBuilder();
93+
int i = 0;
94+
int n = text.length();
95+
while (i < n) {
96+
boolean find = false;
97+
for (int l = 1; l < 8; ++l) {
98+
int j = i + l;
99+
if (j <= n) {
100+
String t = text.substring(i, j);
101+
if (d.containsKey(t)) {
102+
ans.append(d.get(t));
103+
i = j;
104+
find = true;
105+
break;
106+
}
107+
}
108+
}
109+
if (!find) {
110+
ans.append(text.charAt(i++));
111+
}
112+
}
113+
return ans.toString();
114+
}
115+
}
116+
```
61117

118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
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;"] = "/";
131+
string ans = "";
132+
int i = 0, n = text.size();
133+
while (i < n)
134+
{
135+
bool find = false;
136+
for (int l = 1; l < 8; ++l)
137+
{
138+
int j = i + l;
139+
if (j <= n)
140+
{
141+
string t = text.substr(i, l);
142+
if (d.count(t))
143+
{
144+
ans += d[t];
145+
i = j;
146+
find = true;
147+
break;
148+
}
149+
}
150+
}
151+
if (!find) ans += text[i++];
152+
}
153+
return ans;
154+
}
155+
};
62156
```
63157
64158
### **...**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
{
15+
bool find = false;
16+
for (int l = 1; l < 8; ++l)
17+
{
18+
int j = i + l;
19+
if (j <= n)
20+
{
21+
string t = text.substr(i, l);
22+
if (d.count(t))
23+
{
24+
ans += d[t];
25+
i = j;
26+
find = true;
27+
break;
28+
}
29+
}
30+
}
31+
if (!find) ans += text[i++];
32+
}
33+
return ans;
34+
}
35+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public String entityParser(String text) {
3+
Map<String, String> d = new HashMap<>();
4+
d.put("&quot;", "\"");
5+
d.put("&apos;", "'");
6+
d.put("&amp;", "&");
7+
d.put("&gt;", ">");
8+
d.put("&lt;", "<");
9+
d.put("&frasl;", "/");
10+
StringBuilder ans = new StringBuilder();
11+
int i = 0;
12+
int n = text.length();
13+
while (i < n) {
14+
boolean find = false;
15+
for (int l = 1; l < 8; ++l) {
16+
int j = i + l;
17+
if (j <= n) {
18+
String t = text.substring(i, j);
19+
if (d.containsKey(t)) {
20+
ans.append(d.get(t));
21+
i = j;
22+
find = true;
23+
break;
24+
}
25+
}
26+
}
27+
if (!find) {
28+
ans.append(text.charAt(i++));
29+
}
30+
}
31+
return ans.toString();
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def entityParser(self, text: str) -> str:
3+
d = {
4+
'&quot;': '"',
5+
'&apos;': "'",
6+
'&amp;': "&",
7+
"&gt;": '>',
8+
"&lt;": '<',
9+
"&frasl;": '/',
10+
}
11+
i, n = 0, len(text)
12+
ans = []
13+
while i < n:
14+
for l in range(1, 8):
15+
j = i + l
16+
if text[i:j] in d:
17+
ans.append(d[text[i:j]])
18+
i = j
19+
break
20+
else:
21+
ans.append(text[i])
22+
i += 1
23+
return ''.join(ans)

0 commit comments

Comments
 (0)