码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Ugly Number II"

时间:2015-08-25 15:46:11      阅读:158      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!