题解 | #丑数#
丑数
https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b
代码实现
class Solution { public: int GetUglyNumber_Solution(int index) { if(index == 0) return 0; //开辟三个队列 queue<int> q2; queue<int> q3; queue<int> q5; vector<int> arr; int numNew = 1; arr.push_back(numNew); while(arr.size() < index) { q2.push(numNew * 2); q3.push(numNew * 3); q5.push(numNew * 5); numNew = min(q2.front(), min(q3.front(), q5.front())); if(q2.front() == numNew) q2.pop(); if(q3.front() == numNew) q3.pop(); if(q5.front() == numNew) q5.pop(); //最小丑数入丑数队列 arr.push_back(numNew); } return arr[arr.size() - 1]; } };