3242. Design Neighbor Sum Service
You are given a
n x n
2D arraygrid
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 ofvalue
, that is either to the top, left, right, or bottom ofvalue
ingrid
.int diagonalSum(int value)
returns the sum of elements which are diagonal neighbors ofvalue
, that is either to the top-left, top-right, bottom-left, or bottom-right ofvalue
ingrid
.