Problem overview
FindInGrid receives a grid, an int[][], and an int target. It returns the position of the first cell equal to target as a List<int> of two integers: the row, then the column. Rows are numbered from 0 from top to bottom, columns from 0 from left to right. If no cell matches, it returns [-1, -1].
"First" means reading order: the top row from left to right, then the next row, and so on. In the grid [1, 2, 3], [4, 5, 6], [7, 8, 9] the target 6 is in row 1, column 2, so the result is [1, 2]. In [5, 1], [1, 5] the value 1 appears twice, and reading row by row the one in row 0, column 1 comes first, so the result is [0, 1]. In a grid of fours with the target 3, the result is [-1, -1].
Approach: index loops and an early return
The answer is a position, so the loops need the row and column numbers, not just the values. That means for loops with indexes. An outer loop over the row numbers and an inner loop over the column numbers of that row visit the cells in exactly reading order: all of row 0 left to right, then row 1, and so on. The first match the loops meet is therefore the first in reading order, and the method can return it on the spot.
If the loops finish without a match, the method falls out of both of them and returns [-1, -1]. The value -1 can't be mistaken for a real position, since rows and columns start at 0.
Step by step
- Loop over row numbers with
for (int row = 0; row < grid.Length; row++). In anint[][],grid.Lengthis the number of rows. - Inside it, loop over column numbers with
for (int col = 0; col < grid[row].Length; col++).grid[row]is one row, anint[], and itsLengthis the number of columns. The inner loop completes a whole row before the next one starts, which is the reading order the problem asks for. - If
grid[row][col] == target, returnnew List<int> { row, col }. The braces after the constructor list the initial items.returnleaves the whole method, so both loops stop at the first match. Abreakwould stop only the inner loop. - After both loops, return
new List<int> { -1, -1 }. The method only gets here when no cell matched.
The solution in C#
class Solution {
public List<int> FindInGrid(int[][] grid, int target) {
for (int row = 0; row < grid.Length; row++) {
for (int col = 0; col < grid[row].Length; col++) {
if (grid[row][col] == target) {
return new List<int> { row, col };
}
}
}
return new List<int> { -1, -1 };
}
}
C# details:
- Collection initializer.
new List<int> { row, col }creates the list and adds both values in one expression. It is the C# way to build a small list without callingAddtwice. - Row first, then column. In
grid[row][col]the first brackets select a row and the second a value in it. The returned list follows the same order: row, then column. - Each row's own length. The inner bound is
grid[row].Length, notgrid[0].Length. Here rows are all equal, so both work, but a jagged array allows rows of different lengths, and this form is right for them too. - Why not
foreach. Aforeachgives values but no positions, and the answer needs positions. List<T>is inSystem.Collections.Generic; in a file of your own, addusing System.Collections.Generic;.
Edge cases and common mistakes
- Duplicates. Returning at the first match is what makes the result the first in reading order. Continuing the search would find later matches, and recording the last one would give
[1, 0]for the 1 in[[5, 1], [1, 5]]instead of[0, 1]. - Using
breakinstead ofreturn. It leaves only the inner loop, and the outer loop goes on to the next row. - Swapped order. Returning
{ col, row }gives[2, 1]for the target 6 in the first example, which would be a different cell. - No match. Both loops run to the end and the final
returngives[-1, -1]. Don't put that return inside the loops, or it would run on the first cell that doesn't match. - Negative values. They are compared like any others.
Testing
grid | target | Result |
|---|---|---|
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] | 6 | [1, 2] |
[[5, 1], [1, 5]] | 1 | [0, 1] |
[[4, 4], [4, 4]] | 3 | [-1, -1] |
