class Solution { public: int nthUglyNumber(int n) { if(n <= 0) return false; // get rid of corner cases if(n == 1) return true; // base case int t2 = 0, t3 = 0, t5 = 0; //pointers for 2, 3, 5 vector<int> k(n); k[0] = 1; for(int i = 1; i < n ; i ++) { k[i] = min(k[t2]*2,min(k[t3]*3,k[t5]*5)); if(k[i] == k[t2]*2) t2++; if(k[i] == k[t3]*3) t3++; if(k[i] == k[t5]*5) t5++; } return k[n-1]; } };
这里返回值是int,但是和true和false语义完全一致。
时间: 2024-10-10 20:52:41