---
# System prepended metadata

title: 0932. Beautiful Array
tags: [Leetcode, FaceBook, Medium]

---

# 0932. Beautiful Array
###### tags: `Leetcode` `FaceBook` `Medium`
Link: https://leetcode.com/problems/beautiful-array/

## 思路
很变态的一道题 思路参考[这里](https://leetcode.com/problems/beautiful-array/)
简单来说就是通过几个公式来从{1}建构这个array

为什么要通过找一个全是奇数的beautiful array和一个全是偶数的beautiful array合并在一起求解？
![](https://i.imgur.com/Zpz55MT.png)

Beautiful Array Property
![](https://i.imgur.com/aYC5gHY.png)

构建过程
![](https://i.imgur.com/VaVS68n.png)

## Code
```java=
class Solution {
    public int[] beautifulArray(int n) {
        List<Integer> ans = new ArrayList<>();
        ans.add(1);
        while(ans.size()<n){
            List<Integer> temp = new ArrayList<>();
            for(int num:ans){
                if(2*num-1<=n) temp.add(2*num-1);
            }
            for(int num:ans){
                if(2*num<=n) temp.add(2*num);
            }
            ans = temp;
        }
        int[] res = new int[n];
        int index = 0;
        for(int num:ans){
            res[index++] = num;
        }
        return res;
    }
}
```