C++ clean code


  • 0
    O
    class Solution {
    public:
        int findCircleNum(vector<vector<int>>& M) {
            int n=M.size();
            vector<int> visited(n);
            for(int i=0;i<n;i++)
                visited[i]=0;
            int cnt=0;
            for(int i=0;i<n;i++)
            {
                if(visited[i]==0)
                {
                    cnt++;
                    DFS(i,visited,M);
                }
            }
            return cnt;
        }
        void DFS(int i,vector<int>& visited,vector<vector<int>>& M)
        {
            int j;
            visited[i]=1;
            for(j=0;j<visited.size();j++)
            {
                if(M[i][j]==1&&visited[j]==0)
                    DFS(j,visited,M);
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.