Skip to content

Instantly share code, notes, and snippets.

@cschlyter
Created December 20, 2022 20:22
Show Gist options
  • Save cschlyter/3556d9ba34e5c9eb535f3b0388d01a7a to your computer and use it in GitHub Desktop.
Save cschlyter/3556d9ba34e5c9eb535f3b0388d01a7a to your computer and use it in GitHub Desktop.
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
count = 0
length = len(isConnected)
for i in range(length):
if isConnected[i][i] == 1:
count += 1
self.dfs(i, length, isConnected)
return count
def dfs(self, i, length, matrix):
if matrix[i][i] == 0:
return
for j in range(length):
if matrix[i][j] == 1:
matrix[i][j] = 0
self.dfs(j, length, matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment