# 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合并在一起求解?  Beautiful Array Property  构建过程  ## 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up