547. Number of Provinces Medium

@problem@discussion
#Depth-First Search#Breadth-First Search#Union Find#Graph



1/**
2 * [547] Number of Provinces
3 *
4 * There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.
5 * A province is a group of directly or indirectly connected cities and no other cities outside of the group.
6 * You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the i^th city and the j^th city are directly connected, and isConnected[i][j] = 0 otherwise.
7 * Return the total number of provinces.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
11 * Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
12 * Output: 2
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
16 * Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
17 * Output: 3
18 * 
19 *  
20 * Constraints:
21 * 
22 * 	1 <= n <= 200
23 * 	n == isConnected.length
24 * 	n == isConnected[i].length
25 * 	isConnected[i][j] is 1 or 0.
26 * 	isConnected[i][i] == 1
27 * 	isConnected[i][j] == isConnected[j][i]
28 * 
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/number-of-provinces/
33// discuss: https://leetcode.com/problems/number-of-provinces/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38    pub fn find_circle_num(is_connected: Vec<Vec<i32>>) -> i32 {
39        0
40    }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_547() {
51    }
52}
53


Back
© 2025 bowen.ge All Rights Reserved.