partition-array-according-to-given-pivot
1.0.0
Partition Array According to Given Pivot
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
5
class
Solution
6
{
7
public
:
8
vector<int>
pivotArray
(vector<int>& nums,
int
pivot)
9
{
10
queue<int> left;
11
queue<int> right;
12
int
pivot_count = 0;
13
for
(
int
i = 0; i < (int)nums.size(); ++i)
14
{
15
if
(nums[i] < pivot)
16
left.push(nums[i]);
17
else
if
(nums[i] > pivot)
18
right.push(nums[i]);
19
else
20
pivot_count++;
21
}
22
vector<int> answer;
23
answer.reserve(nums.size());
24
while
(!left.empty())
25
{
26
answer.push_back(left.front());
27
left.pop();
28
}
29
for
(
int
i = 0; i < pivot_count; ++i)
30
answer.push_back(pivot);
31
while
(!right.empty())
32
{
33
answer.push_back(right.front());
34
right.pop();
35
}
36
return
answer;
37
}
38
};
39
40
int
main
()
41
{
42
vector<int> nums = {9,12,5,10,14,3,10};
43
// vector<int> nums = {-8,0,7,-7,19,15,6,-5,-10,11,-6,-5,20,3,-6,10,-2};
44
vector<int> answer =
Solution
().
pivotArray
(nums, 10);
45
for
(
int
el: answer)
46
cout << el <<
' '
;
47
cout <<
'\n'
;
48
return
0;
49
}
Solution
Definition
main.cpp:6
Solution::pivotArray
vector< int > pivotArray(vector< int > &nums, int pivot)
Definition
main.cpp:8
main
int main()
Definition
main.cpp:40
main.cpp
Generated by
1.9.8