Skip to content

feat: update solutions to lc problems: No.1436,2161 #3611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 21 additions & 55 deletions solution/1400-1499/1436.Destination City/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tags:

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

Expand Down Expand Up @@ -74,9 +74,9 @@ tags:

### 方法一:哈希表

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

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

<!-- tabs:start -->

Expand All @@ -98,12 +98,12 @@ class Solution {
for (var p : paths) {
s.add(p.get(0));
}
for (var p : paths) {
if (!s.contains(p.get(1))) {
return p.get(1);
for (int i = 0;; ++i) {
var b = paths.get(i).get(1);
if (!s.contains(b)) {
return b;
}
}
return "";
}
}
```
Expand All @@ -118,12 +118,12 @@ public:
for (auto& p : paths) {
s.insert(p[0]);
}
for (auto& p : paths) {
if (!s.count(p[1])) {
return p[1];
for (int i = 0;; ++i) {
auto b = paths[i][1];
if (!s.contains(b)) {
return b;
}
}
return "";
}
};
```
Expand All @@ -149,29 +149,23 @@ func destCity(paths [][]string) string {

```ts
function destCity(paths: string[][]): string {
const set = new Set(paths.map(([a]) => a));
for (const [_, b] of paths) {
if (!set.has(b)) {
return b;
}
}
return '';
const s = new Set<string>(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))![1];
}
```

#### Rust

```rust
use std::collections::HashSet;

impl Solution {
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
for path in paths.iter() {
if !set.contains(&path[1]) {
return path[1].clone();
}
}
String::new()
let s = paths
.iter()
.map(|p| p[0].clone())
.collect::<HashSet<String>>();
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
}
}
```
Expand All @@ -184,39 +178,11 @@ impl Solution {
* @return {string}
*/
var destCity = function (paths) {
const s = new Set();
for (const [a, _] of paths) {
s.add(a);
}
for (const [_, b] of paths) {
if (!s.has(b)) {
return b;
}
}
return '';
const s = new Set(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))[1];
};
```

#### C

```c
char* destCity(char*** paths, int pathsSize, int* pathsColSize) {
for (int i = 0; i < pathsSize; i++) {
int flag = 1;
for (int j = 0; j < pathsSize; j++) {
if (strcmp(paths[i][1], paths[j][0]) == 0) {
flag = 0;
break;
}
}
if (flag) {
return paths[i][1];
}
}
return NULL;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
78 changes: 24 additions & 54 deletions solution/1400-1499/1436.Destination City/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tags:

<pre>
<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;]]
<strong>Output:</strong> &quot;Sao Paulo&quot;
<strong>Output:</strong> &quot;Sao Paulo&quot;
<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;.
</pre>

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

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

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}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of $\textit{paths}$.

<!-- tabs:start -->

Expand All @@ -92,12 +96,12 @@ class Solution {
for (var p : paths) {
s.add(p.get(0));
}
for (var p : paths) {
if (!s.contains(p.get(1))) {
return p.get(1);
for (int i = 0;; ++i) {
var b = paths.get(i).get(1);
if (!s.contains(b)) {
return b;
}
}
return "";
}
}
```
Expand All @@ -112,12 +116,12 @@ public:
for (auto& p : paths) {
s.insert(p[0]);
}
for (auto& p : paths) {
if (!s.count(p[1])) {
return p[1];
for (int i = 0;; ++i) {
auto b = paths[i][1];
if (!s.contains(b)) {
return b;
}
}
return "";
}
};
```
Expand All @@ -143,29 +147,23 @@ func destCity(paths [][]string) string {

```ts
function destCity(paths: string[][]): string {
const set = new Set(paths.map(([a]) => a));
for (const [_, b] of paths) {
if (!set.has(b)) {
return b;
}
}
return '';
const s = new Set<string>(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))![1];
}
```

#### Rust

```rust
use std::collections::HashSet;

impl Solution {
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
for path in paths.iter() {
if !set.contains(&path[1]) {
return path[1].clone();
}
}
String::new()
let s = paths
.iter()
.map(|p| p[0].clone())
.collect::<HashSet<String>>();
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
}
}
```
Expand All @@ -178,39 +176,11 @@ impl Solution {
* @return {string}
*/
var destCity = function (paths) {
const s = new Set();
for (const [a, _] of paths) {
s.add(a);
}
for (const [_, b] of paths) {
if (!s.has(b)) {
return b;
}
}
return '';
const s = new Set(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))[1];
};
```

#### C

```c
char* destCity(char*** paths, int pathsSize, int* pathsColSize) {
for (int i = 0; i < pathsSize; i++) {
int flag = 1;
for (int j = 0; j < pathsSize; j++) {
if (strcmp(paths[i][1], paths[j][0]) == 0) {
flag = 0;
break;
}
}
if (flag) {
return paths[i][1];
}
}
return NULL;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
15 changes: 0 additions & 15 deletions solution/1400-1499/1436.Destination City/Solution.c

This file was deleted.

10 changes: 5 additions & 5 deletions solution/1400-1499/1436.Destination City/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class Solution {
for (auto& p : paths) {
s.insert(p[0]);
}
for (auto& p : paths) {
if (!s.count(p[1])) {
return p[1];
for (int i = 0;; ++i) {
auto b = paths[i][1];
if (!s.contains(b)) {
return b;
}
}
return "";
}
};
};
10 changes: 5 additions & 5 deletions solution/1400-1499/1436.Destination City/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public String destCity(List<List<String>> paths) {
for (var p : paths) {
s.add(p.get(0));
}
for (var p : paths) {
if (!s.contains(p.get(1))) {
return p.get(1);
for (int i = 0;; ++i) {
var b = paths.get(i).get(1);
if (!s.contains(b)) {
return b;
}
}
return "";
}
}
}
12 changes: 2 additions & 10 deletions solution/1400-1499/1436.Destination City/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
* @return {string}
*/
var destCity = function (paths) {
const s = new Set();
for (const [a, _] of paths) {
s.add(a);
}
for (const [_, b] of paths) {
if (!s.has(b)) {
return b;
}
}
return '';
const s = new Set(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))[1];
};
13 changes: 6 additions & 7 deletions solution/1400-1499/1436.Destination City/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::collections::HashSet;

impl Solution {
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
let set = paths.iter().map(|v| &v[0]).collect::<HashSet<&String>>();
for path in paths.iter() {
if !set.contains(&path[1]) {
return path[1].clone();
}
}
String::new()
let s = paths
.iter()
.map(|p| p[0].clone())
.collect::<HashSet<String>>();
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
}
}
9 changes: 2 additions & 7 deletions solution/1400-1499/1436.Destination City/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
function destCity(paths: string[][]): string {
const set = new Set(paths.map(([a]) => a));
for (const [_, b] of paths) {
if (!set.has(b)) {
return b;
}
}
return '';
const s = new Set<string>(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))![1];
}
Loading
Loading