Problem overview
The function centerOfGrid receives a 3x3 grid of integers and returns the number in its center: the one in the middle row and the middle column. A grid is a table of numbers made of rows, and here it has 3 rows with 3 numbers each.
For [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the middle row is [4, 5, 6] and the number in its middle column is 5. For a grid with a 7 at the center and zeros around it, the answer is 7. The center can be any number between -1000 and 1000, and a negative one is fine: in [[9, 8, 7], [6, -5, 4], [3, 2, 1]] the answer is -5.
Approach
In C++, a grid is a vector<vector<int>>: an outer vector whose items are rows, and each row is a vector<int>. Reading a value takes two lookups. The first picks a row, and the second picks a number inside that row.
Indexes start at 0, so the rows are 0, 1 and 2, and the columns are numbered the same way. The middle of three is index 1, both for the row and for the column. The center is therefore at row 1, column 1, and the function doesn't need a loop or a calculation.
Step by step
- Pick the middle row.
grid[1]is the whole second row, avector<int>. - Pick the middle column of that row. Putting a second index after the first,
grid[1][1], reads the number at that row and column. - Return it. The function's return type is
int, which matches the number.
The solution in C++
class Solution {
public:
int centerOfGrid(vector<vector<int>> &grid) {
return grid[1][1];
}
};
Some C++ details worth knowing:
- Row first, then column. In
grid[row][col]the first index chooses the row and the second chooses the position inside it. With the first example,grid[1]is[4, 5, 6], andgrid[1][0]is 4. Swapping the two indexes gives a different cell in general:grid[2][1]is 8, which is in the last row, not at the center. - Passing by reference. The parameter is declared with
&, so the function receives the caller's grid without copying it. For a 3x3 grid the copy would be small, but this is the usual way to pass vectors. The function only reads the grid and doesn't change it. - No bounds check. The
[]operator of a vector doesn't check the index. Reading outside the vector is undefined behaviour, not an exception. The constraints guarantee exactly 3 rows with 3 numbers each, so indexes 1 and 1 are always valid. - A safer alternative.
grid.at(1).at(1)checks both indexes and throwsstd::out_of_rangewhen one is invalid. It behaves the same on valid grids.
Edge cases and common mistakes
- Starting to count at 1.
grid[2][2]is the bottom-right number, which is 9 in the first example. The middle index is 1, because counting starts at 0. - Confusing the rows with the numbers.
grid[1]alone is a row, not anint, and returning it doesn't compile. - Negative values. The center can be negative, and the code returns it unchanged.
- Zeros and equal numbers. A grid full of zeros except for the center, as in the second example, tests that the code reads the right cell: only
grid[1][1]returns 7.
Testing
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]gives 5.[[0, 0, 0], [0, 7, 0], [0, 0, 0]]gives 7.[[9, 8, 7], [6, -5, 4], [3, 2, 1]]gives -5.
