File tree Expand file tree Collapse file tree 5 files changed +283
-2
lines changed
solution/1400-1499/1410.HTML Entity Parser Expand file tree Collapse file tree 5 files changed +283
-2
lines changed Original file line number Diff line number Diff line change 72
72
73
73
<!-- 这里可写通用的实现逻辑 -->
74
74
75
+ ** 方法一:哈希表**
76
+
75
77
<!-- tabs:start -->
76
78
77
79
### ** Python3**
78
80
79
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
82
81
83
``` python
82
-
84
+ class Solution :
85
+ def entityParser (self , text : str ) -> str :
86
+ d = {
87
+ ' "' : ' "' ,
88
+ ' '' : " '" ,
89
+ ' &' : " &" ,
90
+ " >" : ' >' ,
91
+ " <" : ' <' ,
92
+ " ⁄" : ' /' ,
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)
83
107
```
84
108
85
109
### ** Java**
86
110
87
111
<!-- 这里可写当前语言的特殊实现逻辑 -->
88
112
89
113
``` java
114
+ class Solution {
115
+ public String entityParser (String text ) {
116
+ Map<String , String > d = new HashMap<> ();
117
+ d. put(" "" , " \" " );
118
+ d. put(" '" , " '" );
119
+ d. put(" &" , " &" );
120
+ d. put(" >" , " >" );
121
+ d. put(" <" , " <" );
122
+ d. put(" ⁄" , " /" );
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
+ ```
90
148
149
+ ### ** C++**
150
+
151
+ ``` cpp
152
+ class Solution {
153
+ public:
154
+ string entityParser(string text) {
155
+ unordered_map<string, string> d;
156
+ d[ "" ; "] = "\" ";
157
+ d[ "&apos ; "] = "'";
158
+ d[ "& ; "] = "&";
159
+ d[ "> ; "] = ">";
160
+ d[ "< ; "] = "<";
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
+ };
91
187
```
92
188
93
189
### **...**
Original file line number Diff line number Diff line change 52
52
### ** Python3**
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def entityParser (self , text : str ) -> str :
57
+ d = {
58
+ ' "' : ' "' ,
59
+ ' '' : " '" ,
60
+ ' &' : " &" ,
61
+ " >" : ' >' ,
62
+ " <" : ' <' ,
63
+ " ⁄" : ' /' ,
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)
56
78
```
57
79
58
80
### ** Java**
59
81
60
82
``` java
83
+ class Solution {
84
+ public String entityParser (String text ) {
85
+ Map<String , String > d = new HashMap<> ();
86
+ d. put(" "" , " \" " );
87
+ d. put(" '" , " '" );
88
+ d. put(" &" , " &" );
89
+ d. put(" >" , " >" );
90
+ d. put(" <" , " <" );
91
+ d. put(" ⁄" , " /" );
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
+ ```
61
117
118
+ ### ** C++**
119
+
120
+ ``` cpp
121
+ class Solution {
122
+ public:
123
+ string entityParser(string text) {
124
+ unordered_map<string, string> d;
125
+ d[ "" ; "] = "\" ";
126
+ d[ "&apos ; "] = "'";
127
+ d[ "& ; "] = "&";
128
+ d[ "> ; "] = ">";
129
+ d[ "< ; "] = "<";
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
+ };
62
156
```
63
157
64
158
### **...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ string entityParser (string text) {
4
+ unordered_map<string, string> d;
5
+ d[" "" ] = " \" " ;
6
+ d[" '" ] = " '" ;
7
+ d[" &" ] = " &" ;
8
+ d[" >" ] = " >" ;
9
+ d[" <" ] = " <" ;
10
+ d[" ⁄" ] = " /" ;
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
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String entityParser (String text ) {
3
+ Map <String , String > d = new HashMap <>();
4
+ d .put (""" , "\" " );
5
+ d .put ("'" , "'" );
6
+ d .put ("&" , "&" );
7
+ d .put (">" , ">" );
8
+ d .put ("<" , "<" );
9
+ d .put ("⁄" , "/" );
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
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def entityParser (self , text : str ) -> str :
3
+ d = {
4
+ '"' : '"' ,
5
+ ''' : "'" ,
6
+ '&' : "&" ,
7
+ ">" : '>' ,
8
+ "<" : '<' ,
9
+ "⁄" : '/' ,
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 )
You can’t perform that action at this time.
0 commit comments