-
Notifications
You must be signed in to change notification settings - Fork 0
/
2149.cpp
46 lines (45 loc) · 1.05 KB
/
2149.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<utility>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<math.h>
#include<unordered_set>
#include<queue>
using namespace std;
class Solution {
public:
int find(int neg_ptr,vector<int>& nums,int n){
for(int i=neg_ptr;i<n;++i){
if(nums[i]<0){
return i;
}
}
return n;
}
vector<int> rearrangeArray(vector<int>& nums) {
int n=nums.size();
int pos_ptr=0;
int neg_ptr=0;
neg_ptr=find(0,nums,n);
for(int i=0;i<n;++i){
if(i==pos_ptr){
continue;
}
if(nums[i]>0){
if(nums[pos_ptr]>0){
swap(nums[neg_ptr],nums[pos_ptr]);
neg_ptr=find(neg_ptr+1,nums,n);
}
swap(nums[pos_ptr],nums[i]);
pos_ptr+=2;
}
else{
while(i>=neg_ptr){
}
}
}
return nums;
}
};