20200323-劍指offer-面試題49. 醜數

在這裏插入圖片描述
題目鏈接:鏈接

class Solution {
public:
    int nthUglyNumber(int n) {
        vector<int> res;
        res.push_back(1);
        if(n==1) return 1;

        int i=0,j=0,k=0;
        for(int id=0;id<n;id++)
        {
            int ans1=res[i]*2;
            int ans2=res[j]*3;
            int ans3=res[k]*5;
            int tmp=min(ans1,min(ans2,ans3));
            res.push_back(tmp);
            if(tmp==ans1) i++;
            if(tmp==ans2) j++;
            if(tmp==ans3) k++;
        } 
        
        return res[n-1];
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章