Skip to content

Commit

Permalink
add 1410
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Apr 20, 2020
1 parent c43f056 commit a163fad
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,5 @@ LeetCode
|1405|[Longest Happy String](https://leetcode.com/problems/longest-happy-string/)|c|[c++](./src/1405-Longest-Happy-String/1405.cpp)|[python](./src/1405-Longest-Happy-String/1405.py)|[go](./src/1405-Longest-Happy-String/1405.go)|[js](./src/1405-Longest-Happy-String/1405.js)|[java](./src/1405-Longest-Happy-String/1405.java)|Medium|
|1406|[Stone Game III](https://leetcode.com/problems/stone-game-iii/)|c|[c++](./src/1406-Stone-Game-III/1406.cpp)|[python](./src/1406-Stone-Game-III/1406.py)|[go](./src/1406-Stone-Game-III/1406.go)|[js](./src/1406-Stone-Game-III/1406.js)|[java](./src/1406-Stone-Game-III/1406.java)|Hard|
|1408|[String Matching in an Array](String Matching in an Array)|c|[c++](./src/1408-String-Matching-in-an-Array/1408.cpp)|[python](./src/1408-String-Matching-in-an-Array/1408.py)|[go](./src/1408-String-Matching-in-an-Array/1408.go)|[js](./src/1408-String-Matching-in-an-Array/1408.js)|[java](./src/1408-String-Matching-in-an-Array/1408.java)|Easy|
|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|c|[c++](./src/1409-Queries-on-a-Permutation-With-Key/1409.cpp)|[python](./src/1409-Queries-on-a-Permutation-With-Key/1409.py)|[go](./src/1409-Queries-on-a-Permutation-With-Key/1409.go)|[js](./src/1409-Queries-on-a-Permutation-With-Key/1409.js)|[java](./src/1409-Queries-on-a-Permutation-With-Key/1409.java)|Medium|
|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|c|[c++](./src/1409-Queries-on-a-Permutation-With-Key/1409.cpp)|[python](./src/1409-Queries-on-a-Permutation-With-Key/1409.py)|[go](./src/1409-Queries-on-a-Permutation-With-Key/1409.go)|[js](./src/1409-Queries-on-a-Permutation-With-Key/1409.js)|[java](./src/1409-Queries-on-a-Permutation-With-Key/1409.java)|Medium|
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|c|[c++](./src/1410-HTML-Entity-Parser/1410.cpp)|[python](./src/1410-HTML-Entity-Parser/1410.py)|[go](./src/1410-HTML-Entity-Parser/1410.go)|[js](./src/1410-HTML-Entity-Parser/1410.js)|[java](./src/1410-HTML-Entity-Parser/1410.java)|Medium|
30 changes: 30 additions & 0 deletions src/1410-HTML-Entity-Parser/1410.cpp
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 = {
{"&quot", '\"'},
{"&apos", '\''},
{"&gt", '>'},
{"&lt", '<'},
{"&frasl", '/'},
{"&amp", '&'},
};

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();
}
};
33 changes: 33 additions & 0 deletions src/1410-HTML-Entity-Parser/1410.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
func entityParser(text string) string {
entity := map[string]byte {
"&quot": '"',
"&apos": '\'',
"&gt": '>',
"&lt": '<',
"&frasl": '/',
"&amp": '&',
}
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()
}
29 changes: 29 additions & 0 deletions src/1410-HTML-Entity-Parser/1410.java
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("&amp", "&");
entity.put("&quot", "\"");
entity.put("&apos", "'");
entity.put("&gt", ">");
entity.put("&lt", "<");
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();
}
}
25 changes: 25 additions & 0 deletions src/1410-HTML-Entity-Parser/1410.js
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 = {
'&quot': '\"',
'&apos': "'",
'&gt': '>',
'&lt': '<',
'&frasl': '/',
'&amp': '&',
};

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;
};
27 changes: 27 additions & 0 deletions src/1410-HTML-Entity-Parser/1410.py
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 = {
'&quot': '\"',
'&apos': "'",
'&gt': '>',
'&lt': '<',
'&frasl': '/',
'&amp': '&',
}

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)
6 changes: 3 additions & 3 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os, bisect

# 题目名称
name = "Queries on a Permutation With Key"
ID = 1409
url = "https://leetcode.com/problems/queries-on-a-permutation-with-key/"
name = "HTML Entity Parser"
ID = 1410
url = "https://leetcode.com/problems/html-entity-parser/"
difficult = "Medium"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']

Expand Down

0 comments on commit a163fad

Please sign in to comment.