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;
}
标签:Perfect,dist,--,int,push,return,Squares,leetcode 来源: https://blog.csdn.net/qq_20817327/article/details/119271170