forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c43f056
commit a163fad
Showing
7 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution { | ||
public: | ||
string entityParser(string text) { | ||
stringstream res; | ||
int n = text.size(), i = 0; | ||
unordered_map<string, char> entity = { | ||
{""", '\"'}, | ||
{"&apos", '\''}, | ||
{">", '>'}, | ||
{"<", '<'}, | ||
{"&frasl", '/'}, | ||
{"&", '&'}, | ||
}; | ||
|
||
while (i < n) { | ||
if (text[i] == '&') { | ||
stringstream t; | ||
while (text[i] != ';') { | ||
t << text[i++]; | ||
} | ||
|
||
string cur = t.str(); | ||
if (entity.count(cur)) res << entity[cur]; | ||
else res << cur << ";"; | ||
} else res << text[i]; | ||
i++; | ||
} | ||
return res.str(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
func entityParser(text string) string { | ||
entity := map[string]byte { | ||
""": '"', | ||
"&apos": '\'', | ||
">": '>', | ||
"<": '<', | ||
"&frasl": '/', | ||
"&": '&', | ||
} | ||
n, i := len(text), 0 | ||
var res strings.Builder | ||
|
||
for i < n { | ||
if text[i] == '&' { | ||
var t strings.Builder | ||
for text[i] != ';' { | ||
t.WriteByte(text[i]) | ||
i++ | ||
} | ||
|
||
if v, ok := entity[t.String()]; ok { | ||
res.WriteByte(v) | ||
} else { | ||
res.WriteString(t.String()) | ||
res.WriteByte(';') | ||
} | ||
} else { | ||
res.WriteByte(text[i]) | ||
} | ||
i++ | ||
} | ||
return res.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution { | ||
public String entityParser(String text) { | ||
Map<String, String> entity = new HashMap(); | ||
entity.put("&", "&"); | ||
entity.put(""", "\""); | ||
entity.put("&apos", "'"); | ||
entity.put(">", ">"); | ||
entity.put("<", "<"); | ||
entity.put("&frasl", "/"); | ||
|
||
StringBuilder res = new StringBuilder(); | ||
int n = text.length(), i = 0; | ||
while (i < n) { | ||
if (text.charAt(i) == '&') { | ||
StringBuilder t = new StringBuilder(); | ||
while (text.charAt(i) != ';') { | ||
t.append(text.charAt(i)); | ||
i++; | ||
} | ||
|
||
String cur = t.toString(); | ||
if (entity.containsKey(cur)) res.append(entity.get(cur)); | ||
else res.append(cur).append(";"); | ||
} else res.append(text.charAt(i)); | ||
i++; | ||
} | ||
return res.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var entityParser = function(text) { | ||
let res = "", n = text.length, i = 0; | ||
let entity = { | ||
'"': '\"', | ||
'&apos': "'", | ||
'>': '>', | ||
'<': '<', | ||
'&frasl': '/', | ||
'&': '&', | ||
}; | ||
|
||
while (i < n) { | ||
if (text[i] == '&') { | ||
let t = ""; | ||
while (text[i] != ';') { | ||
t += text[i++]; | ||
} | ||
|
||
if (t in entity) res += entity[t]; | ||
else res += t + ";"; | ||
} else res += text[i]; | ||
i++; | ||
} | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution: | ||
def entityParser(self, text: str) -> str: | ||
res = [] | ||
n, i = len(text), 0 | ||
entity = { | ||
'"': '\"', | ||
'&apos': "'", | ||
'>': '>', | ||
'<': '<', | ||
'&frasl': '/', | ||
'&': '&', | ||
} | ||
|
||
while i < n: | ||
if text[i] == '&': | ||
t = '' | ||
while text[i] != ';': | ||
t += text[i] | ||
i += 1 | ||
if t in entity: | ||
res.append(entity[t]) | ||
else: | ||
res.append(t + ";") | ||
else: | ||
res.append(text[i]) | ||
i += 1 | ||
return "".join(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters