-
Notifications
You must be signed in to change notification settings - Fork 0
/
667.hpp
42 lines (37 loc) · 810 Bytes
/
667.hpp
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
#ifndef LEETCODE_667_HPP
#define LEETCODE_667_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> arr(n);
int l = 1, h = n;
int flag = 1, cur = 0;
while (--k > 0) {
if (flag > 0) {
arr[cur] = l++;
} else {
arr[cur] = h--;
}
flag *= -1;
cur++;
}
int r = flag > 0 ? l : h;
while (cur < n) {
arr[cur++] = r;
r += flag;
}
return arr;
}
};
#endif //LEETCODE_667_HPP