Skip to content

Commit b3ed1e2

Browse files
committed
feat: add solutions to lc problem: No.1436
No.1436.Destination City
1 parent ce68e5b commit b3ed1e2

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,57 @@ var destCity = function (paths) {
154154
};
155155
```
156156

157+
### **TypeScript**
158+
159+
```ts
160+
function destCity(paths: string[][]): string {
161+
const set = new Set(paths.map(([a]) => a));
162+
for (const [_, b] of paths) {
163+
if (!set.has(b)) {
164+
return b;
165+
}
166+
}
167+
return '';
168+
}
169+
```
170+
171+
### **Rust**
172+
173+
```rust
174+
use std::collections::HashSet;
175+
impl Solution {
176+
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
177+
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
178+
for path in paths.iter() {
179+
if !set.contains(&path[1]) {
180+
return path[1].clone();
181+
}
182+
}
183+
String::new()
184+
}
185+
}
186+
```
187+
188+
### **C**
189+
190+
```c
191+
char *destCity(char ***paths, int pathsSize, int *pathsColSize) {
192+
for (int i = 0; i < pathsSize; i++) {
193+
int flag = 1;
194+
for (int j = 0; j < pathsSize; j++) {
195+
if (strcmp(paths[i][1], paths[j][0]) == 0) {
196+
flag = 0;
197+
break;
198+
}
199+
}
200+
if (flag) {
201+
return paths[i][1];
202+
}
203+
}
204+
return NULL;
205+
}
206+
```
207+
157208
### **...**
158209
159210
```

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,57 @@ var destCity = function (paths) {
138138
};
139139
```
140140

141+
### **TypeScript**
142+
143+
```ts
144+
function destCity(paths: string[][]): string {
145+
const set = new Set(paths.map(([a]) => a));
146+
for (const [_, b] of paths) {
147+
if (!set.has(b)) {
148+
return b;
149+
}
150+
}
151+
return '';
152+
}
153+
```
154+
155+
### **Rust**
156+
157+
```rust
158+
use std::collections::HashSet;
159+
impl Solution {
160+
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
161+
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
162+
for path in paths.iter() {
163+
if !set.contains(&path[1]) {
164+
return path[1].clone();
165+
}
166+
}
167+
String::new()
168+
}
169+
}
170+
```
171+
172+
### **C**
173+
174+
```c
175+
char *destCity(char ***paths, int pathsSize, int *pathsColSize) {
176+
for (int i = 0; i < pathsSize; i++) {
177+
int flag = 1;
178+
for (int j = 0; j < pathsSize; j++) {
179+
if (strcmp(paths[i][1], paths[j][0]) == 0) {
180+
flag = 0;
181+
break;
182+
}
183+
}
184+
if (flag) {
185+
return paths[i][1];
186+
}
187+
}
188+
return NULL;
189+
}
190+
```
191+
141192
### **...**
142193
143194
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
char *destCity(char ***paths, int pathsSize, int *pathsColSize) {
2+
for (int i = 0; i < pathsSize; i++) {
3+
int flag = 1;
4+
for (int j = 0; j < pathsSize; j++) {
5+
if (strcmp(paths[i][1], paths[j][0]) == 0) {
6+
flag = 0;
7+
break;
8+
}
9+
}
10+
if (flag) {
11+
return paths[i][1];
12+
}
13+
}
14+
return NULL;
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
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()
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
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 '';
9+
}

0 commit comments

Comments
 (0)