首页 > 其他 > 详细

LeetCode "Ugly Number II"

时间:2015-08-25 15:45:39      阅读:192      评论:0      收藏:0      [点我收藏+]

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;
    }
};

LeetCode "Ugly Number II"

原文:http://www.cnblogs.com/tonix/p/4757155.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!