题解 | #丑数#
丑数
https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b
#include <functional> #include <queue> #include <unordered_map> class Solution { public: int GetUglyNumber_Solution(int index) { if(index==0){ return 0; } vector<int> vec={2,3,5}; unordered_map<long, int> map; priority_queue<long,vector<long>,greater<long>> ans; map[1]=1; ans.push(1); long a=0; for(int i=0;i<index;i++){ a=ans.top(); ans.pop(); for(int j=0;j<3;j++){ long t=a*vec[j]; if(map.find(t)==map.end()){ map[t]=1; ans.push(t); } } } return a; } };