#3898
Easy Algorithms Find the degree of each vertex
92.7% acceptance
May 13, 2026
36
0
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] = 1 indicates that there is an edge between vertices i and j.
matrix[i][j] = 0 indicates that there is no edge between vertices i and j.
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn find_degrees(matrix: Vec<Vec<i32>>) -> Vec<i32> {
matrix.iter().map(|row| row.iter().sum()).collect()
}
}