Leetcode 1791. Find the central node calculation degree of star graph

Original title link: Leetcode 1791. Find the central node of the star graph

insert image description here
insert image description here

class Solution {
    
    
public:
    int findCenter(vector<vector<int>>& edges) {
    
    
        int res=0,cnt=0;
        int n=edges.size()+1;
        vector<int> degree(n+1);
        for(auto x:edges){
    
    
            int a=x[0],b=x[1];
            degree[a]++; degree[b]++;
        }
        for(int i=1;i<=n;i++)
        {
    
    
            if(degree[i]==n-1) return i;
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/qq_45791939/article/details/128103737