Skip to content

Commit e4eac2b

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 6343775 + fecf7d9 commit e4eac2b

File tree

43 files changed

+2676
-1241
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2676
-1241
lines changed

solution/0000-0099/0091.Decode Ways/README_EN.md

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,66 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>A message containing letters from <code>A-Z</code> can be <strong>encoded</strong> into numbers using the following mapping:</p>
20+
<p>You have intercepted a secret message encoded as a string of numbers. The message is <strong>decoded</strong> via the following mapping:</p>
2121

22-
<pre>
23-
&#39;A&#39; -&gt; &quot;1&quot;
24-
&#39;B&#39; -&gt; &quot;2&quot;
25-
...
26-
&#39;Z&#39; -&gt; &quot;26&quot;
27-
</pre>
22+
<p><code>&quot;1&quot; -&gt; &#39;A&#39;<br />
23+
&quot;2&quot; -&gt; &#39;B&#39;<br />
24+
...<br />
25+
&quot;25&quot; -&gt; &#39;Y&#39;<br />
26+
&quot;26&quot; -&gt; &#39;Z&#39;</code></p>
2827

29-
<p>To <strong>decode</strong> an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, <code>&quot;11106&quot;</code> can be mapped into:</p>
28+
<p>However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes (<code>&quot;2&quot;</code> and <code>&quot;5&quot;</code> vs <code>&quot;25&quot;</code>).</p>
29+
30+
<p>For example, <code>&quot;11106&quot;</code> can be decoded into:</p>
3031

3132
<ul>
32-
<li><code>&quot;AAJF&quot;</code> with the grouping <code>(1 1 10 6)</code></li>
33-
<li><code>&quot;KJF&quot;</code> with the grouping <code>(11 10 6)</code></li>
33+
<li><code>&quot;AAJF&quot;</code> with the grouping <code>(1, 1, 10, 6)</code></li>
34+
<li><code>&quot;KJF&quot;</code> with the grouping <code>(11, 10, 6)</code></li>
35+
<li>The grouping <code>(1, 11, 06)</code> is invalid because <code>&quot;06&quot;</code> is not a valid code (only <code>&quot;6&quot;</code> is valid).</li>
3436
</ul>
3537

36-
<p>Note that the grouping <code>(1 11 06)</code> is invalid because <code>&quot;06&quot;</code> cannot be mapped into <code>&#39;F&#39;</code> since <code>&quot;6&quot;</code> is different from <code>&quot;06&quot;</code>.</p>
37-
38-
<p>Given a string <code>s</code> containing only digits, return <em>the <strong>number</strong> of ways to <strong>decode</strong> it</em>.</p>
38+
<p>Note: there may be strings that are impossible to decode.<br />
39+
<br />
40+
Given a string s containing only digits, return the <strong>number of ways</strong> to <strong>decode</strong> it. If the entire string cannot be decoded in any valid way, return <code>0</code>.</p>
3941

4042
<p>The test cases are generated so that the answer fits in a <strong>32-bit</strong> integer.</p>
4143

4244
<p>&nbsp;</p>
4345
<p><strong class="example">Example 1:</strong></p>
4446

45-
<pre>
46-
<strong>Input:</strong> s = &quot;12&quot;
47-
<strong>Output:</strong> 2
48-
<strong>Explanation:</strong> &quot;12&quot; could be decoded as &quot;AB&quot; (1 2) or &quot;L&quot; (12).
49-
</pre>
47+
<div class="example-block">
48+
<p><strong>Input:</strong> <span class="example-io">s = &quot;12&quot;</span></p>
49+
50+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
51+
52+
<p><strong>Explanation:</strong></p>
53+
54+
<p>&quot;12&quot; could be decoded as &quot;AB&quot; (1 2) or &quot;L&quot; (12).</p>
55+
</div>
5056

5157
<p><strong class="example">Example 2:</strong></p>
5258

53-
<pre>
54-
<strong>Input:</strong> s = &quot;226&quot;
55-
<strong>Output:</strong> 3
56-
<strong>Explanation:</strong> &quot;226&quot; could be decoded as &quot;BZ&quot; (2 26), &quot;VF&quot; (22 6), or &quot;BBF&quot; (2 2 6).
57-
</pre>
59+
<div class="example-block">
60+
<p><strong>Input:</strong> <span class="example-io">s = &quot;226&quot;</span></p>
61+
62+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
63+
64+
<p><strong>Explanation:</strong></p>
65+
66+
<p>&quot;226&quot; could be decoded as &quot;BZ&quot; (2 26), &quot;VF&quot; (22 6), or &quot;BBF&quot; (2 2 6).</p>
67+
</div>
5868

5969
<p><strong class="example">Example 3:</strong></p>
6070

61-
<pre>
62-
<strong>Input:</strong> s = &quot;06&quot;
63-
<strong>Output:</strong> 0
64-
<strong>Explanation:</strong> &quot;06&quot; cannot be mapped to &quot;F&quot; because of the leading zero (&quot;6&quot; is different from &quot;06&quot;).
65-
</pre>
71+
<div class="example-block">
72+
<p><strong>Input:</strong> <span class="example-io">s = &quot;06&quot;</span></p>
73+
74+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
75+
76+
<p><strong>Explanation:</strong></p>
77+
78+
<p>&quot;06&quot; cannot be mapped to &quot;F&quot; because of the leading zero (&quot;6&quot; is different from &quot;06&quot;). In this case, the string is not a valid encoding, so return 0.</p>
79+
</div>
6680

6781
<p>&nbsp;</p>
6882
<p><strong>Constraints:</strong></p>

solution/0300-0399/0332.Reconstruct Itinerary/README.md

Lines changed: 95 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### 方法一
67+
### 方法一:欧拉路径
68+
69+
题目实际上是给定 $n$ 个点和 $m$ 条边,要求从指定的起点出发,经过所有的边恰好一次,使得路径字典序最小。这是一个典型的欧拉路径问题。
70+
71+
由于本题保证了至少存在一种合理的行程,因此,我们直接利用 Hierholzer 算法,输出从起点出发的欧拉路径即可。
72+
73+
时间复杂度 $O(m \times \log m)$,空间复杂度 $O(m)$。其中 $m$ 是边的数量。
6874

6975
<!-- tabs:start -->
7076

@@ -73,56 +79,43 @@ tags:
7379
```python
7480
class Solution:
7581
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
76-
graph = defaultdict(list)
77-
78-
for src, dst in sorted(tickets, reverse=True):
79-
graph[src].append(dst)
80-
81-
itinerary = []
82-
83-
def dfs(airport):
84-
while graph[airport]:
85-
dfs(graph[airport].pop())
86-
itinerary.append(airport)
87-
82+
def dfs(f: str):
83+
while g[f]:
84+
dfs(g[f].pop())
85+
ans.append(f)
86+
87+
g = defaultdict(list)
88+
for f, t in sorted(tickets, reverse=True):
89+
g[f].append(t)
90+
ans = []
8891
dfs("JFK")
89-
90-
return itinerary[::-1]
92+
return ans[::-1]
9193
```
9294

9395
#### Java
9496

9597
```java
9698
class Solution {
97-
void dfs(Map<String, Queue<String>> adjLists, List<String> ans, String curr) {
98-
Queue<String> neighbors = adjLists.get(curr);
99-
if (neighbors == null) {
100-
ans.add(curr);
101-
return;
102-
}
103-
while (!neighbors.isEmpty()) {
104-
String neighbor = neighbors.poll();
105-
dfs(adjLists, ans, neighbor);
106-
}
107-
ans.add(curr);
108-
return;
109-
}
99+
private Map<String, List<String>> g = new HashMap<>();
100+
private List<String> ans = new ArrayList<>();
110101

111102
public List<String> findItinerary(List<List<String>> tickets) {
112-
Map<String, Queue<String>> adjLists = new HashMap<>();
103+
Collections.sort(tickets, (a, b) -> b.get(1).compareTo(a.get(1)));
113104
for (List<String> ticket : tickets) {
114-
String from = ticket.get(0);
115-
String to = ticket.get(1);
116-
if (!adjLists.containsKey(from)) {
117-
adjLists.put(from, new PriorityQueue<>());
118-
}
119-
adjLists.get(from).add(to);
105+
g.computeIfAbsent(ticket.get(0), k -> new ArrayList<>()).add(ticket.get(1));
120106
}
121-
List<String> ans = new ArrayList<>();
122-
dfs(adjLists, ans, "JFK");
107+
dfs("JFK");
123108
Collections.reverse(ans);
124109
return ans;
125110
}
111+
112+
private void dfs(String f) {
113+
while (g.containsKey(f) && !g.get(f).isEmpty()) {
114+
String t = g.get(f).remove(g.get(f).size() - 1);
115+
dfs(t);
116+
}
117+
ans.add(f);
118+
}
126119
}
127120
```
128121

@@ -132,36 +125,77 @@ class Solution {
132125
class Solution {
133126
public:
134127
vector<string> findItinerary(vector<vector<string>>& tickets) {
135-
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
136-
vector<string> ret;
137-
138-
// Initialize the graph
139-
for (const auto& t : tickets) {
140-
g[t[0]].push(t[1]);
128+
sort(tickets.rbegin(), tickets.rend());
129+
unordered_map<string, vector<string>> g;
130+
for (const auto& ticket : tickets) {
131+
g[ticket[0]].push_back(ticket[1]);
141132
}
133+
vector<string> ans;
134+
auto dfs = [&](auto&& dfs, string& f) -> void {
135+
while (!g[f].empty()) {
136+
string t = g[f].back();
137+
g[f].pop_back();
138+
dfs(dfs, t);
139+
}
140+
ans.emplace_back(f);
141+
};
142+
string f = "JFK";
143+
dfs(dfs, f);
144+
reverse(ans.begin(), ans.end());
145+
return ans;
146+
}
147+
};
148+
```
142149
143-
findItineraryInner(g, ret, "JFK");
150+
#### Go
151+
152+
```go
153+
func findItinerary(tickets [][]string) (ans []string) {
154+
sort.Slice(tickets, func(i, j int) bool {
155+
return tickets[i][0] > tickets[j][0] || (tickets[i][0] == tickets[j][0] && tickets[i][1] > tickets[j][1])
156+
})
157+
g := make(map[string][]string)
158+
for _, ticket := range tickets {
159+
g[ticket[0]] = append(g[ticket[0]], ticket[1])
160+
}
161+
var dfs func(f string)
162+
dfs = func(f string) {
163+
for len(g[f]) > 0 {
164+
t := g[f][len(g[f])-1]
165+
g[f] = g[f][:len(g[f])-1]
166+
dfs(t)
167+
}
168+
ans = append(ans, f)
169+
}
170+
dfs("JFK")
171+
for i := 0; i < len(ans)/2; i++ {
172+
ans[i], ans[len(ans)-1-i] = ans[len(ans)-1-i], ans[i]
173+
}
174+
return
175+
}
176+
```
144177

145-
ret = {ret.rbegin(), ret.rend()};
178+
#### TypeScript
146179

147-
return ret;
180+
```ts
181+
function findItinerary(tickets: string[][]): string[] {
182+
const g: Record<string, string[]> = {};
183+
tickets.sort((a, b) => b[1].localeCompare(a[1]));
184+
for (const [f, t] of tickets) {
185+
g[f] = g[f] || [];
186+
g[f].push(t);
148187
}
149-
150-
void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
151-
if (g.count(cur) == 0) {
152-
// This is the end point
153-
ret.push_back(cur);
154-
return;
155-
} else {
156-
while (!g[cur].empty()) {
157-
auto front = g[cur].top();
158-
g[cur].pop();
159-
findItineraryInner(g, ret, front);
160-
}
161-
ret.push_back(cur);
188+
const ans: string[] = [];
189+
const dfs = (f: string) => {
190+
while (g[f] && g[f].length) {
191+
const t = g[f].pop()!;
192+
dfs(t);
162193
}
163-
}
164-
};
194+
ans.push(f);
195+
};
196+
dfs('JFK');
197+
return ans.reverse();
198+
}
165199
```
166200

167201
<!-- tabs:end -->

0 commit comments

Comments
 (0)