rustgym/leetcode/_932_beautiful_array.rs
1struct Solution;
2
3impl Solution {
4 fn beautiful_array(n: i32) -> Vec<i32> {
5 if n == 1 {
6 vec![1]
7 } else {
8 let left = Self::beautiful_array(n / 2);
9 let right = Self::beautiful_array((n + 1) / 2);
10 left.into_iter()
11 .map(|x| x * 2)
12 .chain(right.into_iter().map(|x| x * 2 - 1))
13 .collect()
14 }
15 }
16}
17
18#[test]
19fn test() {
20 let n = 4;
21 let res = vec![4, 2, 3, 1];
22 assert_eq!(Solution::beautiful_array(n), res);
23}