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..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 @@ -204,4 +204,46 @@ function pivotArray(nums: number[], pivot: number): number[] { + + +### 方法二:双指针 + + + +#### 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;
+}