LeetCode Courses 3898. Find the Degree of Each Vertex
LeetCode

3898. Find the Degree of Each Vertex

We sum each row of the adjacency matrix to read off every vertex's degree in one pass. The row-sum-equals-degree identity is a handy shortcut to pocket for graph problems.

1 chapter Beginner Written walk-through Updated 12 May 2026 Phil · @FirnoxGames
Free

Free for everyone. No account needed.

Start reading →

What you'll learn

1 3898. Find the Degree of Each Vertex 📖 5 min read

We sum each row of the adjacency matrix to read off every vertex's degree in one pass. The row-sum-equals-degree identity is a handy shortcut to pocket for graph problems.

In this chapter
  1. Problem
  2. Loop and count
  3. List comprehension
  4. In-place

Problem

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.

Included with this course
📖Written version of every chapter