您的位置:首页 > 其它

LeetCode "Ugly Number II"

2015-08-25 13:15 197 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: