Ugly number || (pointer)

Find the nth ugly number ( 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];
    }
};
Published 152 original articles · won praise 2 · Views 6440

Guess you like

Origin blog.csdn.net/weixin_43918473/article/details/105447999