醜い数字||(ポインタ)

n番目の醜い数( < = 1690 n <= 1690

class Solution {
public:
    int p[2010];
    int nthUglyNumber(int n) {
        p[1] = 1;
        int h2 = 1,h3 = 1,h5 = 1;
        for(int i = 2;i <= n;++i) {
            int val = min(p[h2]*2LL,min(p[h3]*3LL,p[h5]*5LL));
            /*这里不可用else if,可能有多个数需要++*/
            if(p[h2]*2LL == val) ++h2;
            if(p[h3]*3LL == val) ++h3;
            if(p[h5]*5LL == val) ++h5;
            p[i] = val;
            //printf("%d ",val);
        }
        return p[n];
    }
};
公開された152元の記事 ウォンの賞賛2 ビュー6440

おすすめ

転載: blog.csdn.net/weixin_43918473/article/details/105447999