码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Friend Circles

时间:2018-01-01 11:32:59      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:poll   while   friend   mat   bfs   leetcode   java   component   nec   

原题链接在这里:https://leetcode.com/problems/friend-circles/description/

题目:

There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.

Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.

Example 1:

Input: 
[[1,1,0],
 [1,1,0],
 [0,0,1]]
Output: 2
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle. 
The 2nd student himself is in a friend circle. So return 2.

Example 2:

Input: 
[[1,1,0],
 [1,1,1],
 [0,1,1]]
Output: 1
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends, 
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.

Note:

  1. N is in range [1,200].
  2. M[i][i] = 1 for all students.
  3. If M[i][j] = 1, then M[j][i] = 1.

题解:

Number of Connected Components in an Undirected Graph类似. 可以采用DFS, BFS, Union Find三种方法.

当M[i][j] == 1. 就说明i 和 j是好友. DFS, BFS时标记走过的点即可.

Time Complexity: O(n^2). n = M.length. M上的点最多走两遍.

Space: O(n). 开了visited array标记. 最多用了n层stack.

AC Java:

 1 class Solution {
 2     public int findCircleNum(int[][] M) {
 3         if(M == null || M.length == 0 || M[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int len = M.length;
 8         boolean [] visited = new boolean[len];
 9         int res = 0;
10         for(int i = 0; i<len; i++){
11             if(!visited[i]){
12                 dfs(M, visited, i);
13                 res++;
14             }
15         }
16         
17         return res;
18     }
19     
20     private void dfs(int [][] M, boolean [] visited, int i){
21         for(int j = 0; j<M[0].length; j++){
22             if(!visited[j] && M[i][j]==1){
23                 visited[j] = true;
24                 dfs(M, visited, j);
25             }
26         }
27     }
28 }

BFS可以选择在出queue的时候更改标记.

Time Complexity: O(n^2). n = M.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int findCircleNum(int[][] M) {
 3         if(M == null || M.length == 0 || M[0].length == 0){
 4             return 0;
 5         }
 6         
 7         int res = 0;
 8         int len = M.length;
 9         boolean [] visited = new boolean[len];
10         LinkedList<Integer> que = new LinkedList<Integer>();
11         for(int i = 0; i<len; i++){
12             if(!visited[i]){
13                 res++;
14                 que.add(i);
15                 while(!que.isEmpty()){
16                     int cur = que.poll();
17                     visited[cur] = true;
18                     for(int j = 0; j<len; j++){
19                         if(!visited[j] && M[cur][j]==1){
20                             que.add(j);
21                         }
22                     }
23                 }
24             }
25         }
26         
27         return res;
28     }
29 }

 

LeetCode Friend Circles

标签:poll   while   friend   mat   bfs   leetcode   java   component   nec   

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8165984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!