#3242

Design Neighbor Sum Service

easy · 76.4% accepted · 117 likes · top 87%

array · hash table · design · matrix · simulation

⊣ practice⊣ open on leetcode ↗

Description

You are given a n x n 2D array grid containing distinct elements in the range [0, n2 - 1].

Implement the NeighborSum class:

- NeighborSum(int [][]grid) initializes the object.

- int adjacentSum(int value) returns the sum of elements which are adjacent neighbors of value, that is either to the top, left, right, or bottom of value in grid.

- int diagonalSum(int value) returns the sum of elements which are diagonal neighbors of value, that is either to the top-left, top-right, bottom-left, or bottom-right of value in grid.

Solution