From 106f6bb9fa69b3bd5d6093dcd48371edea459fe1 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 4 Mar 2025 17:52:09 +0300 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2161 --- .../README.md | 42 +++++++++++++++++ .../README_EN.md | 46 ++++++++++++++++++- .../Solution2.js | 11 +++++ .../Solution2.ts | 11 +++++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.js create mode 100644 solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.ts 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 01971fcc94e64..6b653269d9c70 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 @@ -204,4 +204,46 @@ function pivotArray(nums: number[], pivot: number): number[] { + + +### Solution 2: Two pointers + + + +#### TypeScript + +```ts +function pivotArray(nums: number[], pivot: number): number[] { + const n = nums.length; + const res = Array(n).fill(pivot); + + for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) { + if (nums[i] < pivot) res[l++] = nums[i]; + if (nums[j] > pivot) res[r--] = nums[j]; + } + + return res; +} +``` + +#### JavaScript + +```js +function pivotArray(nums, pivot) { + const n = nums.length; + const res = Array(n).fill(pivot); + + for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) { + if (nums[i] < pivot) res[l++] = nums[i]; + if (nums[j] > pivot) res[r--] = nums[j]; + } + + return res; +} +``` + + + + + 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 2322a3b02f6a5..df2a4df3c0f24 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.
@@ -203,4 +203,46 @@ function pivotArray(nums: number[], pivot: number): number[] {
 
 
 
+
+
+### Solution 2: Two pointers
+
+
+
+#### TypeScript
+
+```ts
+function pivotArray(nums: number[], pivot: number): number[] {
+    const n = nums.length;
+    const res = Array(n).fill(pivot);
+
+    for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
+        if (nums[i] < pivot) res[l++] = nums[i];
+        if (nums[j] > pivot) res[r--] = nums[j];
+    }
+
+    return res;
+}
+```
+
+#### JavaScript
+
+```js
+function pivotArray(nums, pivot) {
+    const n = nums.length;
+    const res = Array(n).fill(pivot);
+
+    for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
+        if (nums[i] < pivot) res[l++] = nums[i];
+        if (nums[j] > pivot) res[r--] = nums[j];
+    }
+
+    return res;
+}
+```
+
+
+
+
+
 
diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.js b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.js
new file mode 100644
index 0000000000000..3c3d70ddc574c
--- /dev/null
+++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.js	
@@ -0,0 +1,11 @@
+function pivotArray(nums, pivot) {
+    const n = nums.length;
+    const res = Array(n).fill(pivot);
+
+    for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
+        if (nums[i] < pivot) res[l++] = nums[i];
+        if (nums[j] > pivot) res[r--] = nums[j];
+    }
+
+    return res;
+}
diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.ts b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.ts
new file mode 100644
index 0000000000000..410da4f84843b
--- /dev/null
+++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution2.ts	
@@ -0,0 +1,11 @@
+function pivotArray(nums: number[], pivot: number): number[] {
+    const n = nums.length;
+    const res = Array(n).fill(pivot);
+
+    for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
+        if (nums[i] < pivot) res[l++] = nums[i];
+        if (nums[j] > pivot) res[r--] = nums[j];
+    }
+
+    return res;
+}

From ae69ecf0c1d321eb29e14165af4c2235d3640d73 Mon Sep 17 00:00:00 2001
From: Libin YANG 
Date: Wed, 5 Mar 2025 08:38:39 +0800
Subject: [PATCH 2/2] Translate solution title to Chinese

---
 .../2161.Partition Array According to Given Pivot/README.md     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 6b653269d9c70..bacc8c7a80e5b 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	
@@ -206,7 +206,7 @@ function pivotArray(nums: number[], pivot: number): number[] {
 
 
 
-### Solution 2: Two pointers
+### 方法二:双指针