File tree Expand file tree Collapse file tree 1 file changed +8
-22
lines changed Expand file tree Collapse file tree 1 file changed +8
-22
lines changed Original file line number Diff line number Diff line change 5
5
class Solution {
6
6
public:
7
7
vector<int > rearrangeArray (vector<int >& nums) {
8
- int pos = 0 , neg = 0 ;
9
- auto next_pos = [&nums, &pos]() {
10
- for (; pos < size (nums); ++pos) {
11
- if (nums[pos] > 0 ) {
12
- return nums[pos++];
13
- }
14
- }
15
- return -1 ;
16
- };
17
- auto next_neg = [&nums, &neg]() {
18
- for (; neg < size (nums); ++neg) {
19
- if (nums[neg] < 0 ) {
20
- return nums[neg++];
21
- }
22
- }
23
- return -1 ;
24
- };
25
- vector<int > result;
26
- for (int i = 0 ; i < size (nums); ++i) {
27
- if (i % 2 == 0 ) {
28
- result.emplace_back (next_pos ());
8
+ int pos = 0 , neg = 1 ;
9
+ vector<int > result (size (nums));
10
+ for (const auto & x : nums) {
11
+ if (x > 0 ) {
12
+ result[pos] = x;
13
+ pos += 2 ;
29
14
} else {
30
- result.emplace_back (next_neg ());
15
+ result[neg] = x;
16
+ neg += 2 ;
31
17
}
32
18
}
33
19
return result;
You can’t perform that action at this time.
0 commit comments