Skip to content

Commit 9da264e

Browse files
authored
Improved task 3898
1 parent e4cafa3 commit 9da264e

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

  • src/main/java/g3801_3900/s3898_find_the_degree_of_each_vertex
Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
1-
3898\.
1+
3898\. Find the Degree of Each Vertex
2+
3+
Easy
4+
5+
You are given a 2D integer array `matrix` of size `n x n` representing the adjacency matrix of an undirected graph with `n` vertices labeled from 0 to `n - 1`.
6+
7+
* `matrix[i][j] = 1` indicates that there is an edge between vertices `i` and `j`.
8+
* `matrix[i][j] = 0` indicates that there is no edge between vertices `i` and `j`.
9+
10+
The **degree** of a vertex is the number of edges connected to it.
11+
12+
Return an integer array `ans` of size `n` where `ans[i]` represents the degree of vertex `i`.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2026/03/14/g41f.png)
17+
18+
**Input:** matrix = [[0,1,1],[1,0,1],[1,1,0]]
19+
20+
**Output:** [2,2,2]
21+
22+
**Explanation:**
23+
24+
* Vertex 0 is connected to vertices 1 and 2, so its degree is 2.
25+
* Vertex 1 is connected to vertices 0 and 2, so its degree is 2.
26+
* Vertex 2 is connected to vertices 0 and 1, so its degree is 2.
27+
28+
Thus, the answer is `[2, 2, 2]`.
29+
30+
**Example 2:**
31+
32+
![](https://assets.leetcode.com/uploads/2026/03/14/g42f.png)
33+
34+
**Input:** matrix = [[0,1,0],[1,0,0],[0,0,0]]
35+
36+
**Output:** [1,1,0]
37+
38+
**Explanation:**
39+
40+
* Vertex 0 is connected to vertex 1, so its degree is 1.
41+
* Vertex 1 is connected to vertex 0, so its degree is 1.
42+
* Vertex 2 is not connected to any vertex, so its degree is 0.
43+
44+
Thus, the answer is `[1, 1, 0]`.
45+
46+
**Example 3:**
47+
48+
**Input:** matrix = [[0]]
49+
50+
**Output:** [0]
51+
52+
**Explanation:**
53+
54+
There is only one vertex and it has no edges connected to it. Thus, the answer is `[0]`.
55+
56+
**Constraints:**
57+
58+
* `1 <= n == matrix.length == matrix[i].length <= 100`
59+
* `matrix[i][i] == 0`
60+
* `matrix[i][j]` is either 0 or 1
61+
* `matrix[i][j] == matrix[j][i]`

0 commit comments

Comments
 (0)