From 5f99e5aa10d63383ff373c25d8ae67876d84a598 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 8 Oct 2024 09:38:07 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.1436,2161 * No.1436.Destination City * No.2161.Partition Array According to Given Pivot --- .../1400-1499/1436.Destination City/README.md | 76 +++++------------- .../1436.Destination City/README_EN.md | 78 ++++++------------- .../1436.Destination City/Solution.c | 15 ---- .../1436.Destination City/Solution.cpp | 10 +-- .../1436.Destination City/Solution.java | 10 +-- .../1436.Destination City/Solution.js | 12 +-- .../1436.Destination City/Solution.rs | 13 ++-- .../1436.Destination City/Solution.ts | 9 +-- .../README.md | 50 ++++++++++-- .../README_EN.md | 55 ++++++++++--- .../Solution.cpp | 23 ++++-- .../Solution.ts | 19 +++++ 12 files changed, 189 insertions(+), 181 deletions(-) delete mode 100644 solution/1400-1499/1436.Destination City/Solution.c create mode 100644 solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.ts diff --git a/solution/1400-1499/1436.Destination City/README.md b/solution/1400-1499/1436.Destination City/README.md index 1e07a3b9d376a..4045d36857444 100644 --- a/solution/1400-1499/1436.Destination City/README.md +++ b/solution/1400-1499/1436.Destination City/README.md @@ -30,7 +30,7 @@ tags:
 输入:paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
-输出:"Sao Paulo" 
+输出:"Sao Paulo"
 解释:从 "London" 出发,最后抵达终点站 "Sao Paulo" 。本次旅行的路线是 "London" -> "New York" -> "Lima" -> "Sao Paulo" 。
 
@@ -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}$ 的长度。 @@ -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 ""; } } ``` @@ -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 ""; } }; ``` @@ -149,13 +149,8 @@ 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(paths.map(([a, _]) => a)); + return paths.find(([_, b]) => !s.has(b))![1]; } ``` @@ -163,15 +158,14 @@ function destCity(paths: string[][]): string { ```rust use std::collections::HashSet; + impl Solution { pub fn dest_city(paths: Vec>) -> String { - let set = paths.iter().map(|v| &v[0]).collect::>(); - 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::>(); + paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone() } } ``` @@ -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; -} -``` - diff --git a/solution/1400-1499/1436.Destination City/README_EN.md b/solution/1400-1499/1436.Destination City/README_EN.md index e4f15c0c9368e..f582e96cc51f5 100644 --- a/solution/1400-1499/1436.Destination City/README_EN.md +++ b/solution/1400-1499/1436.Destination City/README_EN.md @@ -29,7 +29,7 @@ tags:
 Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
-Output: "Sao Paulo" 
+Output: "Sao Paulo"
 Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".
 
@@ -70,7 +70,11 @@ Clearly the destination city is "A". -### 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}$. @@ -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 ""; } } ``` @@ -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 ""; } }; ``` @@ -143,13 +147,8 @@ 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(paths.map(([a, _]) => a)); + return paths.find(([_, b]) => !s.has(b))![1]; } ``` @@ -157,15 +156,14 @@ function destCity(paths: string[][]): string { ```rust use std::collections::HashSet; + impl Solution { pub fn dest_city(paths: Vec>) -> String { - let set = paths.iter().map(|v| &v[0]).collect::>(); - 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::>(); + paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone() } } ``` @@ -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; -} -``` - diff --git a/solution/1400-1499/1436.Destination City/Solution.c b/solution/1400-1499/1436.Destination City/Solution.c deleted file mode 100644 index 598c5adf07974..0000000000000 --- a/solution/1400-1499/1436.Destination City/Solution.c +++ /dev/null @@ -1,15 +0,0 @@ -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; -} \ No newline at end of file diff --git a/solution/1400-1499/1436.Destination City/Solution.cpp b/solution/1400-1499/1436.Destination City/Solution.cpp index 86b2dbf592f0e..2d7325ffefe98 100644 --- a/solution/1400-1499/1436.Destination City/Solution.cpp +++ b/solution/1400-1499/1436.Destination City/Solution.cpp @@ -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 ""; } -}; \ No newline at end of file +}; diff --git a/solution/1400-1499/1436.Destination City/Solution.java b/solution/1400-1499/1436.Destination City/Solution.java index 77c8498508bb7..5e86840e0c835 100644 --- a/solution/1400-1499/1436.Destination City/Solution.java +++ b/solution/1400-1499/1436.Destination City/Solution.java @@ -4,11 +4,11 @@ public String destCity(List> 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 ""; } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1436.Destination City/Solution.js b/solution/1400-1499/1436.Destination City/Solution.js index 01c13bb473af2..7d97c683b2f3a 100644 --- a/solution/1400-1499/1436.Destination City/Solution.js +++ b/solution/1400-1499/1436.Destination City/Solution.js @@ -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]; }; diff --git a/solution/1400-1499/1436.Destination City/Solution.rs b/solution/1400-1499/1436.Destination City/Solution.rs index bb33d41b8f055..4bf5f16b9bc5c 100644 --- a/solution/1400-1499/1436.Destination City/Solution.rs +++ b/solution/1400-1499/1436.Destination City/Solution.rs @@ -1,12 +1,11 @@ use std::collections::HashSet; + impl Solution { pub fn dest_city(paths: Vec>) -> String { - let set = paths.iter().map(|v| &v[0]).collect::>(); - 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::>(); + paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone() } } diff --git a/solution/1400-1499/1436.Destination City/Solution.ts b/solution/1400-1499/1436.Destination City/Solution.ts index ca7342d4a141a..cf86124357c1d 100644 --- a/solution/1400-1499/1436.Destination City/Solution.ts +++ b/solution/1400-1499/1436.Destination City/Solution.ts @@ -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(paths.map(([a, _]) => a)); + return paths.find(([_, b]) => !s.has(b))![1]; } diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/README.md b/solution/2100-2199/2161.Partition Array According to Given Pivot/README.md index 6a84347ce1581..efd76ad814049 100644 --- a/solution/2100-2199/2161.Partition Array According to Given Pivot/README.md +++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/README.md @@ -72,7 +72,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们可以遍历数组 $\textit{nums}$,按顺序找出所有小于 $\textit{pivot}$ 的元素,所有等于 $\textit{pivot}$ 的元素,以及所有大于 $\textit{pivot}$ 的元素,然后将它们按照题目要求的顺序拼接起来。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -127,12 +131,21 @@ class Solution { public: vector pivotArray(vector& nums, int pivot) { vector ans; - for (int& x : nums) - if (x < pivot) ans.push_back(x); - for (int& x : nums) - if (x == pivot) ans.push_back(x); - for (int& x : nums) - if (x > pivot) ans.push_back(x); + for (int& x : nums) { + if (x < pivot) { + ans.push_back(x); + } + } + for (int& x : nums) { + if (x == pivot) { + ans.push_back(x); + } + } + for (int& x : nums) { + if (x > pivot) { + ans.push_back(x); + } + } return ans; } }; @@ -162,6 +175,29 @@ func pivotArray(nums []int, pivot int) []int { } ``` +#### TypeScript + +```ts +function pivotArray(nums: number[], pivot: number): number[] { + const ans: number[] = []; + for (const x of nums) { + if (x < pivot) { + ans.push(x); + } + } + if (x === pivot) { + ans.push(x); + } + } + for (const x of nums) { + if (x > pivot) { + ans.push(x); + } + } + return ans; +} +``` + diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/README_EN.md b/solution/2100-2199/2161.Partition Array According to Given Pivot/README_EN.md index 3820f0cc07b51..f5c884470e37a 100644 --- a/solution/2100-2199/2161.Partition Array According to Given Pivot/README_EN.md +++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/README_EN.md @@ -40,7 +40,7 @@ tags:
 Input: nums = [9,12,5,10,14,3,10], pivot = 10
 Output: [9,5,3,10,10,12,14]
-Explanation: 
+Explanation:
 The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
 The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
 The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
@@ -51,7 +51,7 @@ The relative ordering of the elements less than and greater than pivot is also m
 
 Input: nums = [-3,4,3,2], pivot = 2
 Output: [-3,2,4,3]
-Explanation: 
+Explanation:
 The element -3 is less than the pivot so it is on the left side of the array.
 The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
 The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
@@ -72,7 +72,11 @@ The relative ordering of the elements less than and greater than pivot is also m
 
 
 
-### Solution 1
+### Solution 1: Simulation
+
+We can traverse the array $\textit{nums}$, sequentially finding all elements less than $\textit{pivot}$, all elements equal to $\textit{pivot}$, and all elements greater than $\textit{pivot}$, then concatenate them in the order required by the problem.
+
+Time complexity $O(n)$, where $n$ is the length of the array $\textit{nums}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
 
 
 
@@ -127,12 +131,21 @@ class Solution {
 public:
     vector pivotArray(vector& nums, int pivot) {
         vector ans;
-        for (int& x : nums)
-            if (x < pivot) ans.push_back(x);
-        for (int& x : nums)
-            if (x == pivot) ans.push_back(x);
-        for (int& x : nums)
-            if (x > pivot) ans.push_back(x);
+        for (int& x : nums) {
+            if (x < pivot) {
+                ans.push_back(x);
+            }
+        }
+        for (int& x : nums) {
+            if (x == pivot) {
+                ans.push_back(x);
+            }
+        }
+        for (int& x : nums) {
+            if (x > pivot) {
+                ans.push_back(x);
+            }
+        }
         return ans;
     }
 };
@@ -162,6 +175,30 @@ func pivotArray(nums []int, pivot int) []int {
 }
 ```
 
+#### TypeScript
+
+```ts
+function pivotArray(nums: number[], pivot: number): number[] {
+    const ans: number[] = [];
+    for (const x of nums) {
+        if (x < pivot) {
+            ans.push(x);
+        }
+    }
+    for (const x of nums) {
+        if (x === pivot) {
+            ans.push(x);
+        }
+    }
+    for (const x of nums) {
+        if (x > pivot) {
+            ans.push(x);
+        }
+    }
+    return ans;
+}
+```
+
 
 
 
diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp
index 227e94ca4f10d..23954763468f2 100644
--- a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp	
+++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp	
@@ -2,12 +2,21 @@ class Solution {
 public:
     vector pivotArray(vector& nums, int pivot) {
         vector ans;
-        for (int& x : nums)
-            if (x < pivot) ans.push_back(x);
-        for (int& x : nums)
-            if (x == pivot) ans.push_back(x);
-        for (int& x : nums)
-            if (x > pivot) ans.push_back(x);
+        for (int& x : nums) {
+            if (x < pivot) {
+                ans.push_back(x);
+            }
+        }
+        for (int& x : nums) {
+            if (x == pivot) {
+                ans.push_back(x);
+            }
+        }
+        for (int& x : nums) {
+            if (x > pivot) {
+                ans.push_back(x);
+            }
+        }
         return ans;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.ts b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.ts
new file mode 100644
index 0000000000000..c12186e187150
--- /dev/null
+++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.ts	
@@ -0,0 +1,19 @@
+function pivotArray(nums: number[], pivot: number): number[] {
+    const ans: number[] = [];
+    for (const x of nums) {
+        if (x < pivot) {
+            ans.push(x);
+        }
+    }
+    for (const x of nums) {
+        if (x === pivot) {
+            ans.push(x);
+        }
+    }
+    for (const x of nums) {
+        if (x > pivot) {
+            ans.push(x);
+        }
+    }
+    return ans;
+}