Leetcode 1791. 找出星型图的中心节点 (计算定点的度)
作者:互联网
class Solution {
public:
int findCenter(vector<vector<int>>& edges) {
unordered_map<int, int> degree;
int max_degree = 0;
int vertex = 0;
for (auto edge : edges) {
degree[edge[0]]++;
degree[edge[1]]++;
if (degree[edge[0]] >= max_degree) {
vertex = edge[0];
max_degree = degree[edge[0]];
}
if (degree[edge[1]] >= max_degree) {
vertex = edge[1];
max_degree = degree[edge[1]];
}
}
return vertex;
}
};
标签:degree,int,max,1791,vertex,edges,edge,星型,Leetcode 来源: https://blog.csdn.net/wwxy1995/article/details/120576889