Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2161 #4126

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,46 @@ function pivotArray(nums: number[], pivot: number): number[] {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:双指针

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [9,12,5,10,14,3,10], pivot = 10
<strong>Output:</strong> [9,5,3,10,10,12,14]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
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.
Expand All @@ -51,7 +51,7 @@ The relative ordering of the elements less than and greater than pivot is also m
<pre>
<strong>Input:</strong> nums = [-3,4,3,2], pivot = 2
<strong>Output:</strong> [-3,2,4,3]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
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.
Expand Down Expand Up @@ -203,4 +203,46 @@ function pivotArray(nums: number[], pivot: number): number[] {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Two pointers

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}