↓Skip to main content
  1. LeetCode/

LeetCode 3898: Find the Degree of Each Vertex

·1 min· ·
LeetCode Easy Graph Matrix Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-04-12

  • Submissions: Accepted (first try)

Approach
#

matrix[i][j] = 1 means an edge between i and j, so vertex i’s degree is the sum of row i. An undirected graph’s matrix is symmetric; I originally summed each column, which gives the same result.

My Solution
#

class Solution:
    def findDegrees(self, matrix: List[List[int]]) -> List[int]:
        return [sum(row) for row in matrix]

Complexity
#

Time O(n²), space O(n) for the output.

Mistakes to Avoid
#

  • For a directed graph, row sums are outdegrees and column sums are indegrees; they cannot be swapped.

Interview Explanation
#

Row i of the adjacency matrix records i’s neighbors, so each row’s sum is the degree. Time O(n²).

Related

LeetCode 997: Find the Town Judge
·2 mins
LeetCode Easy Array Hash-Table Graph
LeetCode 997 note: the judge is trusted by the other n - 1 people and trusts nobody, so in-degree minus out-degree equals n - 1
LeetCode 892: Surface Area of 3D Shapes
·2 mins
LeetCode Easy Array Math Geometry Matrix
LeetCode 892 note: a stack of v cubes has 4v + 2 faces; subtract 2 × min for each overlap with the stacks above and to the left
LeetCode 883: Projection Area of 3D Shapes
·1 min
LeetCode Easy Array Math Geometry Matrix
LeetCode 883 note: the top view counts nonzero cells, and the front and side views sum each row’s and each column’s maximum
LeetCode 867: Transpose Matrix
·1 min
LeetCode Easy Array Matrix Simulation
LeetCode 867 note: the transpose is n × m with res[j][i] = matrix[i][j]; in Python, zip(*matrix)
LeetCode 832: Flipping an Image
·2 mins
LeetCode Easy Array Two-Pointers Bit-Manipulation Matrix
LeetCode 832 note: reverse each row and invert its bits, which two pointers can do at once
LeetCode 766: Toeplitz Matrix
·2 mins
LeetCode Easy Array Matrix
LeetCode 766 note: every diagonal is constant exactly when each cell equals its upper-left neighbor