Skip to content

Commit b4681e7

Browse files
committed
一刷1410
1 parent 5eb4e5c commit b4681e7

File tree

5 files changed

+117
-38
lines changed

5 files changed

+117
-38
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9896,12 +9896,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
98969896
//|Medium
98979897
//|
98989898

9899-
//|{counter:codes}
9900-
//|{leetcode_base_url}/html-entity-parser/[1410. HTML Entity Parser^]
9901-
//|{source_base_url}/_1410_HtmlEntityParser.java[Java]
9902-
//|{doc_base_url}/1410-html-entity-parser.adoc[题解]
9903-
//|Medium
9904-
//|
9899+
|{counter:codes}
9900+
|{leetcode_base_url}/html-entity-parser/[1410. HTML Entity Parser^]
9901+
|{source_base_url}/_1410_HtmlEntityParser.java[Java]
9902+
|{doc_base_url}/1410-html-entity-parser.adoc[题解]
9903+
|Medium
9904+
|
99059905

99069906
//|{counter:codes}
99079907
//|{leetcode_base_url}/number-of-ways-to-paint-n-3-grid/[1411. Number of Ways to Paint N × 3 Grid^]

docs/1410-html-entity-parser.adoc

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,67 @@
11
[#1410-html-entity-parser]
2-
= 1410. HTML Entity Parser
2+
= 1410. HTML 实体解析器
33

4-
{leetcode}/problems/html-entity-parser/[LeetCode - 1410. HTML Entity Parser ^]
4+
https://leetcode.cn/problems/html-entity-parser/[LeetCode - 1410. HTML 实体解析器 ^]
55

6-
*HTML entity parser* is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.
6+
HTML 实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。
77

8-
The special characters and their entities for HTML are:
8+
HTML 里这些特殊字符和它们对应的字符实体包括:
99

10+
* **双引号:**字符实体为 `"` ,对应的字符是 `"`
11+
* **单引号:**字符实体为 `'` ,对应的字符是 `'`
12+
* **与符号:**字符实体为 `&` ,对应对的字符是 `&`
13+
* **大于号:**字符实体为 `>` ,对应的字符是 `>`
14+
* **小于号:**字符实体为 `&lt;` ,对应的字符是 `<`
15+
* **斜线号:**字符实体为 `&frasl;` ,对应的字符是 `/`
1016
11-
* *Quotation Mark:* the entity is `&amp;quot;` and symbol character is `"`.
12-
* *Single Quote Mark:* the entity is `&amp;apos;` and symbol character is `'`.
13-
* *Ampersand:* the entity is `&amp;amp;` and symbol character is `&amp;`.
14-
* *Greater Than Sign:* the entity is `&amp;gt;` and symbol character is `>`.
15-
* *Less Than Sign:* the entity is `&amp;lt;` and symbol character is `<`.
16-
* *Slash:* the entity is `&amp;frasl;` and symbol character is `/`.
17+
给你输入字符串 `text` ,请你实现一个 HTML 实体解析器,返回解析器解析后的结果。
1718

19+
*示例 1:*
1820

19-
Given the input `text` string to the HTML parser, you have to implement the entity parser.
21+
....
22+
输入:text = "&amp; is an HTML entity but &ambassador; is not."
23+
输出:"& is an HTML entity but &ambassador; is not."
24+
解释:解析器把字符实体 &amp; 用 & 替换
25+
....
2026

21-
Return _the text after replacing the entities by the special characters_.
27+
*示例 2:*
2228

23-
24-
*Example 1:*
29+
....
30+
输入:text = "and I quote: &quot;...&quot;"
31+
输出:"and I quote: \"...\""
32+
....
2533

26-
[subs="verbatim,quotes"]
27-
----
28-
*Input:* text = "&amp;amp; is an HTML entity but &amp;ambassador; is not."
29-
*Output:* "&amp; is an HTML entity but &amp;ambassador; is not."
30-
*Explanation:* The parser will replace the &amp;amp; entity by &amp;
31-
----
34+
*示例 3:*
3235

33-
*Example 2:*
36+
....
37+
输入:text = "Stay home! Practice on Leetcode :)"
38+
输出:"Stay home! Practice on Leetcode :)"
39+
....
3440

35-
[subs="verbatim,quotes"]
36-
----
37-
*Input:* text = "and I quote: &amp;quot;...&amp;quot;"
38-
*Output:* "and I quote: \"...\""
39-
----
41+
*示例 4:*
42+
43+
....
44+
输入:text = "x &gt; y &amp;&amp; x &lt; y is always false"
45+
输出:"x > y && x < y is always false"
46+
....
4047

41-
42-
*Constraints:*
48+
*示例 5:*
4349

50+
....
51+
输入:text = "leetcode.com&frasl;problemset&frasl;all"
52+
输出:"leetcode.com/problemset/all"
53+
....
4454

45-
* `1 <= text.length <= 10^5^`
46-
* The string may contain any possible characters out of all the 256 ASCII characters.
4755

56+
*提示:*
4857

58+
* `+1 <= text.length <= 10^5+`
59+
* 字符串可能包含 256 个ASCII 字符中的任意字符。
4960
5061
5162
== 思路分析
5263

64+
将实体存入 `Map`,然后逐个字符读取,遇到开头 `&` 和结尾 `;` 就专门处理一下。
5365

5466
[[src-1410]]
5567
[tabs]
@@ -76,4 +88,5 @@ include::{sourcedir}/_1410_HtmlEntityParser.java[tag=answer]
7688

7789
== 参考资料
7890

79-
91+
. https://leetcode.cn/problems/html-entity-parser/solutions/2477551/html-shi-ti-jie-xi-qi-by-leetcode-soluti-y851/[1410. HTML 实体解析器 - 官方题解^] -- 在字符串中查找实体,这样处理更简单!
92+
. https://leetcode.cn/problems/html-entity-parser/solutions/2538217/gong-shui-san-xie-jing-dian-zi-fu-chuan-rvdh3/[1410. HTML 实体解析器 - 经典字符串处理与哈希表运用^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2903,7 +2903,7 @@ include::1349-maximum-students-taking-exam.adoc[leveloffset=+1]
29032903

29042904
// include::1409-queries-on-a-permutation-with-key.adoc[leveloffset=+1]
29052905

2906-
// include::1410-html-entity-parser.adoc[leveloffset=+1]
2906+
include::1410-html-entity-parser.adoc[leveloffset=+1]
29072907

29082908
// include::1411-number-of-ways-to-paint-n-3-grid.adoc[leveloffset=+1]
29092909

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,11 @@ endif::[]
778778
|{doc_base_url}/1457-pseudo-palindromic-paths-in-a-binary-tree.adoc[题解]
779779
|✅ 回溯,深度优先遍历,到叶子节点判断是否可以组成回文。
780780

781+
|{counter:codes2503}
782+
|{leetcode_base_url}/html-entity-parser/[1410. HTML 实体解析器^]
783+
|{doc_base_url}/1410-html-entity-parser.adoc[题解]
784+
|✅ 哈希 + 字符串
785+
781786
|===
782787

783788
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Map;
4+
5+
public class _1410_HtmlEntityParser {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-05-22 22:59:23
10+
*/
11+
public String entityParser(String text) {
12+
Map<String, String> map = Map.of(
13+
"&quot;", "\"",
14+
"&apos;", "'",
15+
"&amp;", "&",
16+
"&gt;", ">",
17+
"&lt;", "<",
18+
"&frasl;", "/");
19+
StringBuilder sb = new StringBuilder();
20+
boolean flag = false;
21+
StringBuilder temp = new StringBuilder();
22+
for (int i = 0; i < text.length(); i++) {
23+
char c = text.charAt(i);
24+
if (c == '&') {
25+
if (!temp.isEmpty()) {
26+
sb.append(temp);
27+
temp.setLength(0);
28+
}
29+
temp.append(c);
30+
flag = true;
31+
} else if (c == ';') {
32+
temp.append(c);
33+
String entry = temp.toString();
34+
String s = map.get(entry);
35+
if (s != null) {
36+
sb.append(s);
37+
} else {
38+
sb.append(entry);
39+
}
40+
temp.setLength(0);
41+
flag = false;
42+
} else {
43+
if (flag) {
44+
temp.append(c);
45+
} else {
46+
sb.append(c);
47+
}
48+
}
49+
}
50+
if (!temp.isEmpty()) {
51+
sb.append(temp);
52+
}
53+
return sb.toString();
54+
}
55+
56+
// end::answer[]
57+
public static void main(String[] args) {
58+
new _1410_HtmlEntityParser()
59+
.entityParser("&amp; is an HTML entity but &ambassador; is not.");
60+
}
61+
}

0 commit comments

Comments
 (0)