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
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
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 mInput: 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: vectorpivotArray(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; +}