Skip to content

Commit 57974d8

Browse files
committed
feat: add solutions to lc problem: No.2161
No.2161.Partition Array According to Given Pivot
1 parent 1c569f6 commit 57974d8

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

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

+72-1
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,86 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
class Solution:
67+
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
68+
a, b, c = [], [], []
69+
for x in nums:
70+
if x < pivot:
71+
a.append(x)
72+
elif x == pivot:
73+
b.append(x)
74+
else:
75+
c.append(x)
76+
return a + b + c
6777
```
6878

6979
### **Java**
7080

7181
<!-- 这里可写当前语言的特殊实现逻辑 -->
7282

7383
```java
84+
class Solution {
85+
public int[] pivotArray(int[] nums, int pivot) {
86+
int n = nums.length;
87+
int[] ans = new int[n];
88+
int k = 0;
89+
for (int x : nums) {
90+
if (x < pivot) {
91+
ans[k++] = x;
92+
}
93+
}
94+
for (int x : nums) {
95+
if (x == pivot) {
96+
ans[k++] = x;
97+
}
98+
}
99+
for (int x : nums) {
100+
if (x > pivot) {
101+
ans[k++] = x;
102+
}
103+
}
104+
return ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
vector<int> pivotArray(vector<int>& nums, int pivot) {
115+
vector<int> ans;
116+
for (int& x : nums) if (x < pivot) ans.push_back(x);
117+
for (int& x : nums) if (x == pivot) ans.push_back(x);
118+
for (int& x : nums) if (x > pivot) ans.push_back(x);
119+
return ans;
120+
}
121+
};
122+
```
74123
124+
### **Go**
125+
126+
```go
127+
func pivotArray(nums []int, pivot int) []int {
128+
var ans []int
129+
for _, x := range nums {
130+
if x < pivot {
131+
ans = append(ans, x)
132+
}
133+
}
134+
for _, x := range nums {
135+
if x == pivot {
136+
ans = append(ans, x)
137+
}
138+
}
139+
for _, x := range nums {
140+
if x > pivot {
141+
ans = append(ans, x)
142+
}
143+
}
144+
return ans
145+
}
75146
```
76147

77148
### **TypeScript**

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

+72-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,84 @@ The relative ordering of the elements less than and greater than pivot is also m
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
62+
a, b, c = [], [], []
63+
for x in nums:
64+
if x < pivot:
65+
a.append(x)
66+
elif x == pivot:
67+
b.append(x)
68+
else:
69+
c.append(x)
70+
return a + b + c
6171
```
6272

6373
### **Java**
6474

6575
```java
76+
class Solution {
77+
public int[] pivotArray(int[] nums, int pivot) {
78+
int n = nums.length;
79+
int[] ans = new int[n];
80+
int k = 0;
81+
for (int x : nums) {
82+
if (x < pivot) {
83+
ans[k++] = x;
84+
}
85+
}
86+
for (int x : nums) {
87+
if (x == pivot) {
88+
ans[k++] = x;
89+
}
90+
}
91+
for (int x : nums) {
92+
if (x > pivot) {
93+
ans[k++] = x;
94+
}
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
vector<int> pivotArray(vector<int>& nums, int pivot) {
107+
vector<int> ans;
108+
for (int& x : nums) if (x < pivot) ans.push_back(x);
109+
for (int& x : nums) if (x == pivot) ans.push_back(x);
110+
for (int& x : nums) if (x > pivot) ans.push_back(x);
111+
return ans;
112+
}
113+
};
114+
```
66115
116+
### **Go**
117+
118+
```go
119+
func pivotArray(nums []int, pivot int) []int {
120+
var ans []int
121+
for _, x := range nums {
122+
if x < pivot {
123+
ans = append(ans, x)
124+
}
125+
}
126+
for _, x := range nums {
127+
if x == pivot {
128+
ans = append(ans, x)
129+
}
130+
}
131+
for _, x := range nums {
132+
if x > pivot {
133+
ans = append(ans, x)
134+
}
135+
}
136+
return ans
137+
}
67138
```
68139

69140
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
vector<int> pivotArray(vector<int>& nums, int pivot) {
4+
vector<int> ans;
5+
for (int& x : nums) if (x < pivot) ans.push_back(x);
6+
for (int& x : nums) if (x == pivot) ans.push_back(x);
7+
for (int& x : nums) if (x > pivot) ans.push_back(x);
8+
return ans;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func pivotArray(nums []int, pivot int) []int {
2+
var ans []int
3+
for _, x := range nums {
4+
if x < pivot {
5+
ans = append(ans, x)
6+
}
7+
}
8+
for _, x := range nums {
9+
if x == pivot {
10+
ans = append(ans, x)
11+
}
12+
}
13+
for _, x := range nums {
14+
if x > pivot {
15+
ans = append(ans, x)
16+
}
17+
}
18+
return ans
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[] pivotArray(int[] nums, int pivot) {
3+
int n = nums.length;
4+
int[] ans = new int[n];
5+
int k = 0;
6+
for (int x : nums) {
7+
if (x < pivot) {
8+
ans[k++] = x;
9+
}
10+
}
11+
for (int x : nums) {
12+
if (x == pivot) {
13+
ans[k++] = x;
14+
}
15+
}
16+
for (int x : nums) {
17+
if (x > pivot) {
18+
ans[k++] = x;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
3+
a, b, c = [], [], []
4+
for x in nums:
5+
if x < pivot:
6+
a.append(x)
7+
elif x == pivot:
8+
b.append(x)
9+
else:
10+
c.append(x)
11+
return a + b + c

0 commit comments

Comments
 (0)