class Solution {
public:
int nthUglyNumber(int n) {
vector<int> ans(1, 1);
int multi[3] = { 0,0,0 }, s[3] = { 2,3,5 };//2,3,5
while (ans.size() < n) {
int k = 0;
for (int i = 1; i < 3; ++i)
if (s[k] * ans[multi[k]] > s[i] * ans[multi[i]]) k = i;
if (s[k] * ans[multi[k]] == ans.back()) { ++multi[k]; continue; }
ans.push_back(s[k] * ans[multi[k]++]);
}
return ans.back();
}
};
Leetcode 264. 丑数 II
最新推荐文章于 2024-04-18 07:58:16 发布