Beautiful Arrangement II LeetCode solution in Java

1
class Solution {
2
public int[] constructArray(int n, int k) {
3
int[] result = new int[n];
4
result[0] = 1;
5
int sign = 1;
6
for (int i = 1; i < n; i++, k--) {
7
if (k > 0) {
8
result[i] = result[i - 1] + k * sign;
9
sign *= -1;
10
} else {
11
result[i] = i + 1;
12
}
13
}
14
return result;
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0