/*
* @lc app=leetcode id=932 lang=cpp
*
* [932] Beautiful Array
*/
// @lc code=start
class Solution {
public:
vector<int> beautifulArray(int n) {
vector<int> ans = {1};
while(ans.size() < n) {
vector<int> tmp;
for(int i=0;i<ans.size();i++) if( ans[i] * 2 - 1 <= n) tmp.push_back(ans[i] * 2 - 1);
for(int i=0;i<ans.size();i++) if( ans[i] * 2 <= n) tmp.push_back(ans[i] * 2);
ans = tmp;
}
return ans;
}
};
// @lc code=end
No.291 - LeetCode[932] Beautiful Array - 构造题
LeetCode 932题解
最新推荐文章于 2025-11-24 14:44:53 发布
本文提供了一种使用C++实现的LeetCode 932题“Beautiful Array”的解决方案。通过递归构建的方式,从初始数组{1}
578

被折叠的 条评论
为什么被折叠?



