标签:
Key: each of 2\3\5 is trying to multiply with the least number it has not been multiplied.
class Solution { public: int nthUglyNumber(int n) { if(n< 1) return 0; int ret = 1, i2 = 0, i3= 0, i5 = 0; vector<int> buf; while(--n) { buf.push_back(ret); int v2 = buf[i2] * 2; int v3 = buf[i3] * 3; int v5 = buf[i5] * 5; ret = min(v2, min(v3, v5)); i2 += ret == v2; i3 += ret == v3; i5 += ret == v5; } return ret; } };
标签:
原文地址:http://www.cnblogs.com/tonix/p/4757155.html