Problem overview
The method centerOfGrid receives grid, a 3x3 grid of integers, and returns the number in its center: the one in the middle row and the middle column. The grid has exactly 3 rows with 3 numbers each, and every number is between -1000 and 1000.
For {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} the middle row is {4, 5, 6} and its middle number is 5. For a grid with a 7 in the middle surrounded by zeros, the answer is 7. For {{9, 8, 7}, {6, -5, 4}, {3, 2, 1}} it is -5: the center can be any number, including a negative one.
Approach: two indexes
In Java, a grid of integers has the type int[][]: an array whose items are themselves int[] arrays, one per row. This has two consequences for reading values.
grid[row]is a whole row, anint[].grid[row][col]is one number: first pick the row, then pick the column inside that row.
The first index always selects the row and the second selects the column. The picture to keep in mind is the grid as it is written in the statement, with the first inner array on top.
Indexes start at 0, so a 3x3 grid has rows 0, 1 and 2 and columns 0, 1 and 2. The middle of 0, 1, 2 is 1, in both directions. The center is therefore at row 1, column 1.
Step by step
- Understand the shape. For
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},grid[0]is{1, 2, 3},grid[1]is{4, 5, 6}andgrid[2]is{7, 8, 9}. - Pick the middle row.
grid[1]is the row{4, 5, 6}. - Pick the middle column of that row. Indexing that row with
[1]gives 5. Written in one go, this isgrid[1][1]. - Return it. The method returns an
int, andgrid[1][1]is anint, so the value can be returned directly.
The solution in Java
class Solution {
public int centerOfGrid(int[][] grid) {
return grid[1][1];
}
}
The method returns the following for the examples:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 5
[[9, 8, 7], [6, -5, 4], [3, 2, 1]] -> -5
A few details about Java arrays are worth knowing:
- Arrays have a fixed size. The size of an array is its
length, a field with no parentheses, unlikeString.length(). For this grid,grid.lengthis 3 (the number of rows) andgrid[0].lengthis 3 (the number of columns). - Rows are separate arrays. In Java a 2D array doesn't have to be rectangular, so each row has its own length. This lesson promises 3 and 3, which is why a fixed
[1][1]is safe. - Out-of-range indexes fail at run time. Reading
grid[3][0]throws anArrayIndexOutOfBoundsException. Java never silently returns a default value for a bad index.
Edge cases and common mistakes
- Counting from 1. Beginners often ask for row 2, column 2 because the middle is "the second". Index 2 is the third row, and
grid[2][2]is the bottom-right corner: 9 in the first example. - Swapping the indexes. For a center,
grid[1][1]reads the same either way, so the mistake stays hidden here. For other cells it matters:grid[0][2]is the top-right corner (3), whilegrid[2][0]is the bottom-left (7). - Dividing by two. A general rule for the middle index is
length / 2with integer division, which is 1 for a length of 3. Here the grid is always 3x3, so the constant 1 is enough, but with another size the constant would be wrong. - Misreading the value. The center is a position, not the biggest or the median number. In the second example the center is -5 even though the other numbers are larger.
- Grids of a different shape. With a 2x2 grid,
grid[1][1]is the bottom-right number and there is no center. The constraints exclude such inputs.
Testing
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}gives 5.{{9, 8, 7}, {6, -5, 4}, {3, 2, 1}}gives -5.
