Skip to content

Commit 4574242

Browse files
authored
feat: add solutions to lc problem: No.2161 (#4126)
1 parent 2bbd0ff commit 4574242

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

solution/2100-2199/2161.Partition Array According to Given Pivot/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,46 @@ function pivotArray(nums: number[], pivot: number): number[] {
204204

205205
<!-- solution:end -->
206206

207+
<!-- solution:start -->
208+
209+
### 方法二:双指针
210+
211+
<!-- tabs:start -->
212+
213+
#### TypeScript
214+
215+
```ts
216+
function pivotArray(nums: number[], pivot: number): number[] {
217+
const n = nums.length;
218+
const res = Array(n).fill(pivot);
219+
220+
for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
221+
if (nums[i] < pivot) res[l++] = nums[i];
222+
if (nums[j] > pivot) res[r--] = nums[j];
223+
}
224+
225+
return res;
226+
}
227+
```
228+
229+
#### JavaScript
230+
231+
```js
232+
function pivotArray(nums, pivot) {
233+
const n = nums.length;
234+
const res = Array(n).fill(pivot);
235+
236+
for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
237+
if (nums[i] < pivot) res[l++] = nums[i];
238+
if (nums[j] > pivot) res[r--] = nums[j];
239+
}
240+
241+
return res;
242+
}
243+
```
244+
245+
<!-- tabs:end -->
246+
247+
<!-- solution:end -->
248+
207249
<!-- problem:end -->

solution/2100-2199/2161.Partition Array According to Given Pivot/README_EN.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ tags:
4040
<pre>
4141
<strong>Input:</strong> nums = [9,12,5,10,14,3,10], pivot = 10
4242
<strong>Output:</strong> [9,5,3,10,10,12,14]
43-
<strong>Explanation:</strong>
43+
<strong>Explanation:</strong>
4444
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
4545
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
4646
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
5151
<pre>
5252
<strong>Input:</strong> nums = [-3,4,3,2], pivot = 2
5353
<strong>Output:</strong> [-3,2,4,3]
54-
<strong>Explanation:</strong>
54+
<strong>Explanation:</strong>
5555
The element -3 is less than the pivot so it is on the left side of the array.
5656
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
5757
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[] {
203203

204204
<!-- solution:end -->
205205

206+
<!-- solution:start -->
207+
208+
### Solution 2: Two pointers
209+
210+
<!-- tabs:start -->
211+
212+
#### TypeScript
213+
214+
```ts
215+
function pivotArray(nums: number[], pivot: number): number[] {
216+
const n = nums.length;
217+
const res = Array(n).fill(pivot);
218+
219+
for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
220+
if (nums[i] < pivot) res[l++] = nums[i];
221+
if (nums[j] > pivot) res[r--] = nums[j];
222+
}
223+
224+
return res;
225+
}
226+
```
227+
228+
#### JavaScript
229+
230+
```js
231+
function pivotArray(nums, pivot) {
232+
const n = nums.length;
233+
const res = Array(n).fill(pivot);
234+
235+
for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
236+
if (nums[i] < pivot) res[l++] = nums[i];
237+
if (nums[j] > pivot) res[r--] = nums[j];
238+
}
239+
240+
return res;
241+
}
242+
```
243+
244+
<!-- tabs:end -->
245+
246+
<!-- solution:end -->
247+
206248
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function pivotArray(nums, pivot) {
2+
const n = nums.length;
3+
const res = Array(n).fill(pivot);
4+
5+
for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
6+
if (nums[i] < pivot) res[l++] = nums[i];
7+
if (nums[j] > pivot) res[r--] = nums[j];
8+
}
9+
10+
return res;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function pivotArray(nums: number[], pivot: number): number[] {
2+
const n = nums.length;
3+
const res = Array(n).fill(pivot);
4+
5+
for (let i = 0, l = 0, j = n - 1, r = n - 1; i < n; i++, j--) {
6+
if (nums[i] < pivot) res[l++] = nums[i];
7+
if (nums[j] > pivot) res[r--] = nums[j];
8+
}
9+
10+
return res;
11+
}

0 commit comments

Comments
 (0)