leecode第二百七十九题(完全平方数)

class Solution {
public:
    int numSquares(int n) {
        vector<int> res(n+1,0);
        for(int i=1;i<n+1;i++)
        {
            res[i]=i;
            for(int j=1;i-j*j>=0;j++)
                res[i]=min(res[i],res[i-j*j]+1);
        }
        
        return res[n];
    }
};

分析:

 

posted @ 2019-08-05 11:50  深夜十二点三十三  阅读(96)  评论(0编辑  收藏  举报