Find the Degree of Each Vertex Problem
#3898
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3898.
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.
matrix[i][j] = 1indicates that there is an edge between verticesiandj.matrix[i][j] = 0indicates that there is no edge between verticesiandj.
The degree of a vertex is the number of edges connected to it.
Return an integer array ans of size n where ans[i] represents the degree of vertex i.
Example 1:

Input: matrix = [[0,1,1],[1,0,1],[1,1,0]]
Output: [2,2,2]
Explanation:
- Vertex 0 is connected to vertices 1 and 2, so its degree is 2.
- Vertex 1 is connected to vertices 0 and 2, so its degree is 2.
- Vertex 2 is connected to vertices 0 and 1, so its degree is 2.
Thus, the answer is [2, 2, 2].
Example 2:

Input: matrix = [[0,1,0],[1,0,0],[0,0,0]]
Output: [1,1,0]
Explanation:
- Vertex 0 is connected to vertex 1, so its degree is 1.
- Vertex 1 is connected to vertex 0, so its degree is 1.
- Vertex 2 is not connected to any vertex, so its degree is 0.
Thus, the answer is [1, 1, 0].
Example 3:
Input: matrix = [[0]]
Output: [0]
Explanation:
There is only one vertex and it has no edges connected to it. Thus, the answer is [0].
Constraints:
1 <= n == matrix.length == matrix[i].length <= 100matrix[i][i] == 0matrix[i][j]is either 0 or 1matrix[i][j] == matrix[j][i]
Solution
Approach: Simulation
In an adjacency matrix, row i has a 1 for every neighbor of vertex i. The degree of vertex i is therefore the sum of row i.
Complexity:
- Time complexity:
O(n^2). - Space complexity:
O(1)besides the output, wherenis the number of vertices.
class Solution:
def findDegrees(self, matrix: List[List[int]]) -> List[int]:
ans = [0] * len(matrix)
for i, row in enumerate(matrix):
for x in row:
ans[i] += x
return ans