Golang dfs, 53ms


  • 0
    D

    Translated Java to Golang.
    Original solution by Vinod

    func findCircleNum(M [][]int) int {
        var visited = make([]int, len(M))
        count := 0
        for i := 0; i < len(M); i ++ {
            if visited[i] == 0 {
                dfs(M, visited, i)
                count++
            }
        }
        return count
    }
    
    
    func dfs(M [][]int, visited []int, i int) {
        for j := 0; j < len(M); j ++ {
            if (M[i][j] == 1 && visited[j] == 0) {
                visited[j] = 1
                dfs(M, visited, j)
            }
        }
    }
    

Log in to reply
 

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