friend java_[LeetCode] 547. Friend Circles Java

题目:

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:

N is in range [1,200].

M[i][i] = 1 for all students.

If M[i][j] = 1, then M[j][i] = 1.

题意及分析:求朋友圈个数,若M[i][j]==1,则i,j为朋友,朋友有传递性,直接朋友或者间接朋友组成一个朋友圈。按照图论的方式来讲给出一个图的邻接矩阵,求不相邻的子图个数。我第一次用了宽度遍历,对于其中一个点M[i][j],若该点未被遍历过,则把该点标记为已遍历,然后遍历第i,j行和i,j列(即宽度遍历),找到为1的且未被遍历过的点,重复操作。但是此方法时间复杂度较高,排名基本上在最后面了。。。 另一种是声明一个visited,用于记录遍历过的结点。每次dfs找到一个原矩阵为1的位置(除了对角线),就把这个位置的列数变成行数再dfs,如果是在同一个圈里,最终会绕回已经遍历过的行,visited为true,return 0;如果不是同一个圈,则增加1。(mat[i][j]==1这个判断相当于i的邻接点,深度优先遍历)

代码:

public classSolution {public int findCircleNum(int[][] M) {int res = 0;int row=M.length;if(row==0) return 0;for(int i=0;i

for(int j=0;j<=i;j++){if(M[i][j]==1){ //未被遍历过的值为1的点

res++;boolean[] rowVisited = new boolean[row]; //记录已经遍历过的行

boolean[] colVisited = new boolean[row]; //记录已经遍历过的列

Integer[] index ={i,j};

Queue queue = new LinkedList<>();

queue.add(index);//查找该个点组成的朋友圈,能和该点组成朋友圈的点满足在i,j行或者i,j列

while(!queue.isEmpty()){

Integer[] val=queue.poll();int x =val[0],y=val[1];if(M[x][y]==1){ //防止重复

M[x][y] = 2; //将遍历过的点置为2

if(!rowVisited[x]){for(int m=0;m<=x;m++){ //x行

Integer[] addIndex ={x,m};if(M[x][m]==1 && !queue.contains(addIndex)) queue.add(addIndex);

}

rowVisited[x]=true;

}if(!rowVisited[y]){for(int n=0;n<=y;n++){ //第y行

Integer[] addIndex ={y,n};if(M[y][n]==1) queue.add(addIndex);

}

rowVisited[y]=true;

}if(!colVisited[x]){for(int m=x;m

Integer[] addIndex ={m,x};if(M[m][x]==1&& !queue.contains(addIndex)) queue.add(addIndex);

}

colVisited[x]=true;

}if(!colVisited[y]){for(int m=y;m

Integer[] addIndex ={m,y};if(M[m][y]==1&& !queue.contains(addIndex)) queue.add(addIndex);

}

colVisited[y]=true;

}

}

}

}

}

}returnres;

}

}

代码:

public classSolution {public int findCircleNum(int[][] M) {if (M == null && M.length == 0)return 0;int n =M.length;boolean[] visited = new boolean[n];int count = 0;//如果dfs大于0,说明有未遍历的结点//只需要遍历所有结点

for (int i = 0; i < n; i++)if (dfs(M, i, visited) > 0)

count++;returncount;

}private int dfs(int[][] mat, int i, boolean[] visited) {if(visited[i])return 0;

visited[i]= true;int count = 1;for (int j = 0; j < visited.length; j++)if (i != j && mat[i][j] == 1)

count+=dfs(mat, j, visited);returncount;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动互联网的到来,处理信息不再受制于地理位置的限制,处理信息及时高效,备受人们的喜爱。所以各大互联网厂商都瞄准移动互联网这个潮流进行各大布局,经过多年的大浪淘沙,各种移动操作系统的不断面世,而目前市场占有率最高的就是微信小程序,本次开发一套基于微信小程序的生签到系统,有管理员,教师,学生三个角色。管理员功能有个人中心,学生管理,教师管理,签到管理,学生签到管理,班课信息管理,加入班课管理,请假信息管理,审批信息管理,销假信息管理,系统管理。教师和学生都可以在微信端注册和登录,教师可以管理签到信息,管理班课信息,审批请假信息,查看学生签到,查看加入班级,查看审批信息和销假信息。学生可以查看教师发布的学生签到信息,可以自己选择加入班课信息,添加请假信息,查看审批信息,进行销假操作。基于微信小程序的生签到系统服务端用Java开发的网站后台,接收并且处理微信小程序端传入的json数据,数据库用到了MySQL数据库作为数据的存储。这样就让用户用着方便快捷,都通过同一个后台进行业务处理,而后台又可以根据并发量做好部署,用硬件和软件进行协作,满足于数据的交互式处理,让用户的数据存储更安全,得到数据更方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值