In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to solve the problem of moments in leetcode. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
There are N students in the class. Some of them are friends and some are not. Their friendship is transitive. If we know that An is a friend of B and B is a friend of C, then we can think that An is also a friend of C. The so-called moments refers to the collection of all friends.
Given a matrix M of N * N, it represents the friend relationship between the middle school students in the class. If M [I] [j] = 1, it means that the I and j students are known to be friends with each other, otherwise they do not know. You must output the total number of known moments among all the students.
Example 1:
Input: [[1jue 1jue 0], [1jre 1jue 0], [0je 0pr 1]] output: 2
Explanation: it is known that student 0 and student 1 are friends with each other, and they are in a circle of friends.
The second student himself is in a circle of friends. So return 2.
Example 2:
Input: [[1meme1pyrin0], [1pyrrine 1], [0mem1pyrrine 1]] output: 1
Explanation: it is known that student 0 and student 1 are friends with each other, and student 1 and student 2 are friends with each other, so student 0 and student 2 are also friends, so the three of them are in a circle of friends and return 1.
Note:
N is within the range of [1200].
For all students, M [I] [I] = 1.
If there is M [I] [j] = 1, then M [j] [I] = 1.
Problem-solving ideas
1. A given matrix can be regarded as the adjacency matrix of a graph. In this way, our problem can be changed into the number of connected blocks of an undirected graph.
2. The algorithms for calculating connected blocks are depth first, breadth first and union search set.
3. The idea is the same: dye the connected parts until there is no connected point, and you get a connected block.
4, traverse the entire matrix and get all the connected blocks.
Code implementation:
Func findCircleNum (M [] [] int) int {count:=0 visited:=make ([] int,len (M)) for iRu (M)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.