Created
July 14, 2017 00:47
-
-
Save jianminchen/12acbe6bbb88a4228f4d171a09dbff66 to your computer and use it in GitHub Desktop.
Leetcode 547 - friend circle - JavaScript code to study.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[][]} M | |
* @return {number} | |
*/ | |
var findCircleNum = function(M) { | |
let numCircles = 0 | |
const visited = new Set () | |
M.forEach ((row, i) => { | |
if (visited.has(i)) return | |
numCircles++ | |
const queue = row.map((x, i) => x ? i : -1) . filter ((x) => x > -1) | |
while (queue.length > 0) { | |
const f = queue.pop () | |
if (!visited.has(f)) { | |
visited.add (f) | |
queue.push(... M[f].map((x, i) => x ? i : -1) . filter ((x) => x > -1)) | |
} | |
} | |
}) | |
return numCircles | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment