← 返回 amazon 的题目列表Friend Circle Detection
类型:online_judge
Given a NxN matrix representing the friend relations among N people, where if person A is a friend of person B, then matrix[A][B] = 1, otherwise 0. Friendships are transitive. Determine the number of friend circles.
Input:
An NxN 2D matrix matrix, where 1 <= N <= 200.
Output:
An integer representing the number of friend circles.
Test cases:
Input: [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
Input: [[1,1,0],[1,1,1],[0,1,1]]
Output: 1
Input: [[1,0],[0,1]]
Output: 2
Input: [[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]]
Output: 1
Input: [[1]]
Output: 1
Example
Input
[[1,1,0],[1,1,0],[0,0,1]]