1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: int findCircleNum(vector<vector<int>>& isConnected) { int len = isConnected.size(); int count = 0; vector<int>flag(len,0); for(int i=0;i<len;i++){ if(!flag[i]){ count++; queue<int>found; found.push(i); flag[i]=1; while(!found.empty()){ int cur = found.front(); found.pop(); for(int j=0;j<len;j++){ if(!flag[j]&&isConnected[cur][j]){ found.push(j); flag[j]=1; } } } } } return count; } };
|