leetcode算法题--Perfect Squares

原题链接:https://leetcode.com/problems/perfect-squares/

int numSquares(int n) { //0点到n点的最短距离
    queue<int> q;
    vector<int> dist(n + 1, INT_MAX);
    q.push(0);
    dist[0] = 0;
    while (q.size()) {
        int t = q.front();
        q.pop();
        if(t == n) return dist[n];
        for (int i = 1; i * i + t <= n; i ++ ) {
            int j = i * i + t;
            if (dist[j] >= dist[t] + 1) {
                dist[j] = dist[t] + 1;
                q.push(j);
            } 
        }
    }
    return 0;
}
上一篇:7组-Alpha冲刺-5/6


下一篇:linux 压缩当前文件夹下所有文件