#3242

Design Neighbor Sum Service

newbie · 230 · lc easy +17 · 76.4% accepted · 117 likes · top 87%

Description

You are given n x n 2D array grid with distinct values in [0, n2 - 1]. Implement the NeighborSum class:

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

- int adjacentSum(int value): returns the sum of cells directly above, below, left, and right of the cell containing value.

- int diagonalSum(int value): returns the sum of diagonal neighbors of the cell containing value.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16