Skip to content

Commit 85f6a37

Browse files
committed
feat: add solutions to lc problems: No.0648,0736
* No.0648.Replace Words * No.0736.Parse Lisp Expression
1 parent 8b172ed commit 85f6a37

File tree

5 files changed

+409
-0
lines changed

5 files changed

+409
-0
lines changed

solution/0600-0699/0648.Replace Words/README.md

+85
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:哈希表**
54+
5355
**方法一:前缀树**
5456

5557
<!-- tabs:start -->
@@ -58,6 +60,19 @@
5860

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

63+
```python
64+
class Solution:
65+
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
66+
s = set(dictionary)
67+
words = sentence.split()
68+
for i, word in enumerate(words):
69+
for j in range(1, len(word) + 1):
70+
if word[:j] in s:
71+
words[i] = word[:j]
72+
break
73+
return ' '.join(words)
74+
```
75+
6176
```python
6277
class Trie:
6378
def __init__(self):
@@ -93,6 +108,26 @@ class Solution:
93108

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

111+
```java
112+
class Solution {
113+
public String replaceWords(List<String> dictionary, String sentence) {
114+
Set<String> s = new HashSet<>(dictionary);
115+
String[] words = sentence.split(" ");
116+
for (int i = 0; i < words.length; ++i) {
117+
String word = words[i];
118+
for (int j = 1; j <= word.length(); ++j) {
119+
String t = word.substring(0, j);
120+
if (s.contains(t)) {
121+
words[i] = t;
122+
break;
123+
}
124+
}
125+
}
126+
return String.join(" ", words);
127+
}
128+
}
129+
```
130+
96131
```java
97132
class Trie {
98133
Trie[] children = new Trie[26];
@@ -132,6 +167,36 @@ class Solution {
132167

133168
### **C++**
134169

170+
```cpp
171+
class Solution {
172+
public:
173+
string replaceWords(vector<string>& dictionary, string sentence) {
174+
unordered_set<string> s(dictionary.begin(), dictionary.end());
175+
istringstream is(sentence);
176+
vector<string> words;
177+
string ss;
178+
while (is >> ss) words.push_back(ss);
179+
for (int i = 0; i < words.size(); ++i)
180+
{
181+
string word = words[i];
182+
for (int j = 1; j <= word.size(); ++j)
183+
{
184+
string t = word.substr(0, j);
185+
if (s.count(t))
186+
{
187+
words[i] = t;
188+
break;
189+
}
190+
}
191+
}
192+
string ans = "";
193+
for (string& word : words) ans += word + " ";
194+
ans.pop_back();
195+
return ans;
196+
}
197+
};
198+
```
199+
135200
```cpp
136201
class Trie {
137202
public:
@@ -183,6 +248,26 @@ public:
183248

184249
### **Go**
185250

251+
```go
252+
func replaceWords(dictionary []string, sentence string) string {
253+
s := map[string]bool{}
254+
for _, v := range dictionary {
255+
s[v] = true
256+
}
257+
words := strings.Split(sentence, " ")
258+
for i, word := range words {
259+
for j := 1; j <= len(word); j++ {
260+
t := word[:j]
261+
if s[t] {
262+
words[i] = t
263+
break
264+
}
265+
}
266+
}
267+
return strings.Join(words, " ")
268+
}
269+
```
270+
186271
```go
187272
func replaceWords(dictionary []string, sentence string) string {
188273
trie := &Trie{}

solution/0600-0699/0648.Replace Words/README_EN.md

+83
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@
4646

4747
### **Python3**
4848

49+
```python
50+
class Solution:
51+
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
52+
s = set(dictionary)
53+
words = sentence.split()
54+
for i, word in enumerate(words):
55+
for j in range(1, len(word) + 1):
56+
if word[:j] in s:
57+
words[i] = word[:j]
58+
break
59+
return ' '.join(words)
60+
```
61+
4962
```python
5063
class Trie:
5164
def __init__(self):
@@ -79,6 +92,26 @@ class Solution:
7992

8093
### **Java**
8194

95+
```java
96+
class Solution {
97+
public String replaceWords(List<String> dictionary, String sentence) {
98+
Set<String> s = new HashSet<>(dictionary);
99+
String[] words = sentence.split(" ");
100+
for (int i = 0; i < words.length; ++i) {
101+
String word = words[i];
102+
for (int j = 1; j <= word.length(); ++j) {
103+
String t = word.substring(0, j);
104+
if (s.contains(t)) {
105+
words[i] = t;
106+
break;
107+
}
108+
}
109+
}
110+
return String.join(" ", words);
111+
}
112+
}
113+
```
114+
82115
```java
83116
class Trie {
84117
Trie[] children = new Trie[26];
@@ -118,6 +151,36 @@ class Solution {
118151

119152
### **C++**
120153

154+
```cpp
155+
class Solution {
156+
public:
157+
string replaceWords(vector<string>& dictionary, string sentence) {
158+
unordered_set<string> s(dictionary.begin(), dictionary.end());
159+
istringstream is(sentence);
160+
vector<string> words;
161+
string ss;
162+
while (is >> ss) words.push_back(ss);
163+
for (int i = 0; i < words.size(); ++i)
164+
{
165+
string word = words[i];
166+
for (int j = 1; j <= word.size(); ++j)
167+
{
168+
string t = word.substr(0, j);
169+
if (s.count(t))
170+
{
171+
words[i] = t;
172+
break;
173+
}
174+
}
175+
}
176+
string ans = "";
177+
for (string& word : words) ans += word + " ";
178+
ans.pop_back();
179+
return ans;
180+
}
181+
};
182+
```
183+
121184
```cpp
122185
class Trie {
123186
public:
@@ -169,6 +232,26 @@ public:
169232

170233
### **Go**
171234

235+
```go
236+
func replaceWords(dictionary []string, sentence string) string {
237+
s := map[string]bool{}
238+
for _, v := range dictionary {
239+
s[v] = true
240+
}
241+
words := strings.Split(sentence, " ")
242+
for i, word := range words {
243+
for j := 1; j <= len(word); j++ {
244+
t := word[:j]
245+
if s[t] {
246+
words[i] = t
247+
break
248+
}
249+
}
250+
}
251+
return strings.Join(words, " ")
252+
}
253+
```
254+
172255
```go
173256
func replaceWords(dictionary []string, sentence string) string {
174257
trie := &Trie{}

solution/0700-0799/0736.Parse Lisp Expression/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,88 @@ public:
292292
};
293293
```
294294

295+
### **Go**
296+
297+
```go
298+
func evaluate(expression string) int {
299+
i, n := 0, len(expression)
300+
scope := map[string][]int{}
301+
302+
parseVar := func() string {
303+
j := i
304+
for ; i < n && expression[i] != ' ' && expression[i] != ')'; i++ {
305+
}
306+
return expression[j:i]
307+
}
308+
309+
parseInt := func() int {
310+
sign, v := 1, 0
311+
if expression[i] == '-' {
312+
sign = -1
313+
i++
314+
}
315+
for ; i < n && expression[i] >= '0' && expression[i] <= '9'; i++ {
316+
v = (v * 10) + int(expression[i]-'0')
317+
}
318+
return sign * v
319+
}
320+
321+
var eval func() int
322+
eval = func() int {
323+
if expression[i] != '(' {
324+
if unicode.IsLower(rune(expression[i])) {
325+
t := scope[parseVar()]
326+
return t[len(t)-1]
327+
}
328+
return parseInt()
329+
}
330+
i++
331+
ans := 0
332+
if expression[i] == 'l' {
333+
i += 4
334+
vars := []string{}
335+
for {
336+
v := parseVar()
337+
if expression[i] == ')' {
338+
t := scope[v]
339+
ans = t[len(t)-1]
340+
break
341+
}
342+
i++
343+
vars = append(vars, v)
344+
scope[v] = append(scope[v], eval())
345+
i++
346+
if !unicode.IsLower(rune(expression[i])) {
347+
ans = eval()
348+
break
349+
}
350+
}
351+
for _, v := range vars {
352+
scope[v] = scope[v][:len(scope[v])-1]
353+
}
354+
} else {
355+
add := expression[i] == 'a'
356+
if add {
357+
i += 4
358+
} else {
359+
i += 5
360+
}
361+
a := eval()
362+
i++
363+
b := eval()
364+
if add {
365+
ans = a + b
366+
} else {
367+
ans = a * b
368+
}
369+
}
370+
i++
371+
return ans
372+
}
373+
return eval()
374+
}
375+
```
376+
295377
### **...**
296378

297379
```

0 commit comments

Comments
 (0)