Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of moments when browsing leetcode by golang

Shulou Source: shulou.com Published: 2022-06-01 12:48:40 10月01日 Update

This article mainly shows you "golang leetcode skills how to solve moments problems", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "golang leetcode skills how to solve moments problems" 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:

Enter:

[[1,1,0]

[1,1,0]

[0,0,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:

Enter:

[[1,1,0]

[1,1,1]

[0,1,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.

Ideas for solving the problem:

1, because a friend's friend is still a friend, the first thing to think of is depth-first traversal.

The above is all the contents of this article entitled "how to solve moments problems with golang leetcode skills". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Friends students skills questions content articles output examples learning help input three middle school middle school between famous learning ideas total easy to understand more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple MySQL Shulou Tech Info vpn