做題筆記 2019/10/7

1.在構造函數中需要初始化列表初始化的有如下三種情況:帶有const修飾的類成員,如const int a ;引用成員數據,如 int& p;帶有引用的類變量.

2.醜數o(n)算法,實在想不出o(nlogn)算法,大佬太強了。

class Solution {
public:
    /**
     * @param n: An integer
     * @return: return a  integer as description.
     */
    int nthUglyNumber(int n) 
    {
        // write your code here
        std::vector<int> res;
        int p2 = 0;
        int p3 = 0;
        int p5 = 0;
        if(n < 7)
            return n;
        res.push_back(1);
        for(int i = 0; i < n; i++)
        {
            int tmp = min(min(res[p2] * 2, res[p3] * 3), res[p5] * 5);
            if(res[p2] * 2 == tmp)
                p2++;
            if(res[p3] * 3 == tmp)
                p3++;
            if(res[p5] * 5 == tmp)
                p5++;
            res.push_back(tmp);
        }
        return res[n - 1];
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章