Easy C++ solution 22ms only


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

Log in to reply
 

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