Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2383396

Browse files
feat: update solutions to lc problems: No.1436,2161 (doocs#3611)
* No.1436.Destination City * No.2161.Partition Array According to Given Pivot
1 parent db3c3ef commit 2383396

File tree

12 files changed

+189
-181
lines changed

12 files changed

+189
-181
lines changed

‎solution/1400-1499/1436.Destination City/README.md‎

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tags:
3030

3131
<pre>
3232
<strong>输入:</strong>paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
33-
<strong>输出:</strong>"Sao Paulo"
33+
<strong>输出:</strong>"Sao Paulo"
3434
<strong>解释:</strong>从 "London" 出发,最后抵达终点站 "Sao Paulo" 。本次旅行的路线是 "London" -&gt; "New York" -&gt; "Lima" -&gt; "Sao Paulo" 。
3535
</pre>
3636

@@ -74,9 +74,9 @@ tags:
7474

7575
### 方法一:哈希表
7676

77-
将所有起点存入哈希表中,然后遍历所有终点,找出没出现在哈希表中的终点,即为答案
77+
根据题目描述,终点一定不会出现在所有 $\textit{cityA}$ 中,因此,我们可以先遍历一遍 $\textit{paths},ドル将所有 $\textit{cityA}$ 放入一个集合 $\textit{s}$ 中,然后再遍历一遍 $\textit{paths},ドル找到不在 $\textit{s}$ 中的 $\textit{cityB}$ 即可
7878

79-
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是线路数
79+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为 $\textit{paths}$ 的长度
8080

8181
<!-- tabs:start -->
8282

@@ -98,12 +98,12 @@ class Solution {
9898
for (var p : paths) {
9999
s.add(p.get(0));
100100
}
101-
for (var p : paths) {
102-
if (!s.contains(p.get(1))) {
103-
return p.get(1);
101+
for (int i = 0;; ++i) {
102+
var b = paths.get(i).get(1);
103+
if (!s.contains(b)) {
104+
return b;
104105
}
105106
}
106-
return "";
107107
}
108108
}
109109
```
@@ -118,12 +118,12 @@ public:
118118
for (auto& p : paths) {
119119
s.insert(p[0]);
120120
}
121-
for (auto& p : paths) {
122-
if (!s.count(p[1])) {
123-
return p[1];
121+
for (int i = 0;; ++i) {
122+
auto b = paths[i][1];
123+
if (!s.contains(b)) {
124+
return b;
124125
}
125126
}
126-
return "";
127127
}
128128
};
129129
```
@@ -149,29 +149,23 @@ func destCity(paths [][]string) string {
149149

150150
```ts
151151
function destCity(paths: string[][]): string {
152-
const set = new Set(paths.map(([a]) => a));
153-
for (const [_, b] of paths) {
154-
if (!set.has(b)) {
155-
return b;
156-
}
157-
}
158-
return '';
152+
const s = new Set<string>(paths.map(([a, _]) => a));
153+
return paths.find(([_, b]) => !s.has(b))![1];
159154
}
160155
```
161156

162157
#### Rust
163158

164159
```rust
165160
use std::collections::HashSet;
161+
166162
impl Solution {
167163
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
168-
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
169-
for path in paths.iter() {
170-
if !set.contains(&path[1]) {
171-
return path[1].clone();
172-
}
173-
}
174-
String::new()
164+
let s = paths
165+
.iter()
166+
.map(|p| p[0].clone())
167+
.collect::<HashSet<String>>();
168+
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
175169
}
176170
}
177171
```
@@ -184,39 +178,11 @@ impl Solution {
184178
* @return {string}
185179
*/
186180
var destCity = function (paths) {
187-
const s = new Set();
188-
for (const [a, _] of paths) {
189-
s.add(a);
190-
}
191-
for (const [_, b] of paths) {
192-
if (!s.has(b)) {
193-
return b;
194-
}
195-
}
196-
return '';
181+
const s = new Set(paths.map(([a, _]) => a));
182+
return paths.find(([_, b]) => !s.has(b))[1];
197183
};
198184
```
199185

200-
#### C
201-
202-
```c
203-
char* destCity(char*** paths, int pathsSize, int* pathsColSize) {
204-
for (int i = 0; i < pathsSize; i++) {
205-
int flag = 1;
206-
for (int j = 0; j < pathsSize; j++) {
207-
if (strcmp(paths[i][1], paths[j][0]) == 0) {
208-
flag = 0;
209-
break;
210-
}
211-
}
212-
if (flag) {
213-
return paths[i][1];
214-
}
215-
}
216-
return NULL;
217-
}
218-
```
219-
220186
<!-- tabs:end -->
221187

222188
<!-- solution:end -->

‎solution/1400-1499/1436.Destination City/README_EN.md‎

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929

3030
<pre>
3131
<strong>Input:</strong> paths = [[&quot;London&quot;,&quot;New York&quot;],[&quot;New York&quot;,&quot;Lima&quot;],[&quot;Lima&quot;,&quot;Sao Paulo&quot;]]
32-
<strong>Output:</strong> &quot;Sao Paulo&quot;
32+
<strong>Output:</strong> &quot;Sao Paulo&quot;
3333
<strong>Explanation:</strong> Starting at &quot;London&quot; city you will reach &quot;Sao Paulo&quot; city which is the destination city. Your trip consist of: &quot;London&quot; -&gt; &quot;New York&quot; -&gt; &quot;Lima&quot; -&gt; &quot;Sao Paulo&quot;.
3434
</pre>
3535

@@ -70,7 +70,11 @@ Clearly the destination city is &quot;A&quot;.
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Hash Table
74+
75+
According to the problem description, the destination city will not appear in any of the $\textit{cityA}$. Therefore, we can first traverse the $\textit{paths}$ and put all $\textit{cityA}$ into a set $\textit{s}$. Then, we traverse the $\textit{paths}$ again to find the $\textit{cityB}$ that is not in $\textit{s}$.
76+
77+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of $\textit{paths}$.
7478

7579
<!-- tabs:start -->
7680

@@ -92,12 +96,12 @@ class Solution {
9296
for (var p : paths) {
9397
s.add(p.get(0));
9498
}
95-
for (var p : paths) {
96-
if (!s.contains(p.get(1))) {
97-
return p.get(1);
99+
for (int i = 0;; ++i) {
100+
var b = paths.get(i).get(1);
101+
if (!s.contains(b)) {
102+
return b;
98103
}
99104
}
100-
return "";
101105
}
102106
}
103107
```
@@ -112,12 +116,12 @@ public:
112116
for (auto& p : paths) {
113117
s.insert(p[0]);
114118
}
115-
for (auto& p : paths) {
116-
if (!s.count(p[1])) {
117-
return p[1];
119+
for (int i = 0;; ++i) {
120+
auto b = paths[i][1];
121+
if (!s.contains(b)) {
122+
return b;
118123
}
119124
}
120-
return "";
121125
}
122126
};
123127
```
@@ -143,29 +147,23 @@ func destCity(paths [][]string) string {
143147

144148
```ts
145149
function destCity(paths: string[][]): string {
146-
const set = new Set(paths.map(([a]) => a));
147-
for (const [_, b] of paths) {
148-
if (!set.has(b)) {
149-
return b;
150-
}
151-
}
152-
return '';
150+
const s = new Set<string>(paths.map(([a, _]) => a));
151+
return paths.find(([_, b]) => !s.has(b))![1];
153152
}
154153
```
155154

156155
#### Rust
157156

158157
```rust
159158
use std::collections::HashSet;
159+
160160
impl Solution {
161161
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
162-
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
163-
for path in paths.iter() {
164-
if !set.contains(&path[1]) {
165-
return path[1].clone();
166-
}
167-
}
168-
String::new()
162+
let s = paths
163+
.iter()
164+
.map(|p| p[0].clone())
165+
.collect::<HashSet<String>>();
166+
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
169167
}
170168
}
171169
```
@@ -178,39 +176,11 @@ impl Solution {
178176
* @return {string}
179177
*/
180178
var destCity = function (paths) {
181-
const s = new Set();
182-
for (const [a, _] of paths) {
183-
s.add(a);
184-
}
185-
for (const [_, b] of paths) {
186-
if (!s.has(b)) {
187-
return b;
188-
}
189-
}
190-
return '';
179+
const s = new Set(paths.map(([a, _]) => a));
180+
return paths.find(([_, b]) => !s.has(b))[1];
191181
};
192182
```
193183

194-
#### C
195-
196-
```c
197-
char* destCity(char*** paths, int pathsSize, int* pathsColSize) {
198-
for (int i = 0; i < pathsSize; i++) {
199-
int flag = 1;
200-
for (int j = 0; j < pathsSize; j++) {
201-
if (strcmp(paths[i][1], paths[j][0]) == 0) {
202-
flag = 0;
203-
break;
204-
}
205-
}
206-
if (flag) {
207-
return paths[i][1];
208-
}
209-
}
210-
return NULL;
211-
}
212-
```
213-
214184
<!-- tabs:end -->
215185

216186
<!-- solution:end -->

‎solution/1400-1499/1436.Destination City/Solution.c‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎solution/1400-1499/1436.Destination City/Solution.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ class Solution {
55
for (auto& p : paths) {
66
s.insert(p[0]);
77
}
8-
for (auto& p : paths) {
9-
if (!s.count(p[1])) {
10-
return p[1];
8+
for (int i = 0;; ++i) {
9+
auto b = paths[i][1];
10+
if (!s.contains(b)) {
11+
return b;
1112
}
1213
}
13-
return "";
1414
}
15-
};
15+
};

‎solution/1400-1499/1436.Destination City/Solution.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ public String destCity(List<List<String>> paths) {
44
for (var p : paths) {
55
s.add(p.get(0));
66
}
7-
for (var p : paths) {
8-
if (!s.contains(p.get(1))) {
9-
return p.get(1);
7+
for (int i = 0;; ++i) {
8+
var b = paths.get(i).get(1);
9+
if (!s.contains(b)) {
10+
return b;
1011
}
1112
}
12-
return "";
1313
}
14-
}
14+
}

‎solution/1400-1499/1436.Destination City/Solution.js‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
* @return {string}
44
*/
55
var destCity = function (paths) {
6-
const s = new Set();
7-
for (const [a, _] of paths) {
8-
s.add(a);
9-
}
10-
for (const [_, b] of paths) {
11-
if (!s.has(b)) {
12-
return b;
13-
}
14-
}
15-
return '';
6+
const s = new Set(paths.map(([a, _]) => a));
7+
return paths.find(([_, b]) => !s.has(b))[1];
168
};
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::collections::HashSet;
2+
23
impl Solution {
34
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
4-
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
5-
for path in paths.iter() {
6-
if !set.contains(&path[1]) {
7-
return path[1].clone();
8-
}
9-
}
10-
String::new()
5+
let s = paths
6+
.iter()
7+
.map(|p| p[0].clone())
8+
.collect::<HashSet<String>>();
9+
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
1110
}
1211
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
function destCity(paths: string[][]): string {
2-
const set = new Set(paths.map(([a]) => a));
3-
for (const [_, b] of paths) {
4-
if (!set.has(b)) {
5-
return b;
6-
}
7-
}
8-
return '';
2+
const s = new Set<string>(paths.map(([a, _]) => a));
3+
return paths.find(([_, b]) => !s.has(b))![1];
94
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /