Problem overview
The function findInGrid receives grid, a vector<vector<int>>, and an integer target. It finds the first cell equal to target and returns its position as a vector<int> of two numbers: the row, then the column. Rows are numbered from 0 at the top and columns from 0 at the left. "First" means reading order: the top row from left to right, then the next row. When no cell matches, the function returns [-1, -1].
In [[1, 2, 3], [4, 5, 6], [7, 8, 9]] with target = 6, the answer is [1, 2]: row 1, column 2. In [[5, 1], [1, 5]] with target = 1, the value appears twice, and the answer is [0, 1] because that cell comes first in reading order. In [[4, 4], [4, 4]] with target = 3 the answer is [-1, -1].
Approach: loop with indices, return at the first match
The function must report a position, so it needs the row and column numbers and not only the values. That means index-based loops: an outer loop over row, and an inner loop over col. Going through the columns of a row from left to right before moving to the next row is exactly the reading order the problem asks for, so the first match the loops meet is the right one.
When a cell matches, the function returns at once. A return leaves the whole function, so both loops stop. After the loops, the function knows that no cell matched, and it returns the sentinel [-1, -1]. No real position has a negative number, so the caller can tell it apart from a find.
Step by step
- Loop over the row numbers. Use an
intnamedrowfrom 0 whilerow < grid.size(). The grid is a vector of rows, sogrid.size()is the number of rows. - Loop over the column numbers of this row. Use an
intnamedcolfrom 0 whilecol < grid[row].size().grid[row]is one row, and its size is the number of columns. - Compare the cell with the target. If
grid[row][col] == target, return{row, col}. The braces build thevector<int>from the two numbers. - Return
{-1, -1}after both loops. This line is reached only when nothing matched.
The solution in C++
class Solution {
public:
vector<int> findInGrid(vector<vector<int>> &grid, int target) {
for (int row = 0; row < grid.size(); row++) {
for (int col = 0; col < grid[row].size(); col++) {
if (grid[row][col] == target) {
return {row, col};
}
}
}
return {-1, -1};
}
};
C++ details:
- Returning a braced list.
return {row, col};creates the returnedvector<int>directly from twointvalues. The function's return type tells the compiler what the braces mean. returnversusbreak. Abreakwould stop only the inner loop, and the outer loop would carry on to the next row. Areturnends the function, which is what a "first match" search needs.- Indices instead of range loops. A range-based loop gives values, not positions. When the answer includes a position, the loop variables must be the indices.
- Unsigned sizes.
grid.size()is unsigned androwis anint. The comparison works for these small sizes, though compilers may warn about it.
Complexity
In the worst case, when the target is in the last cell or missing, the function checks every cell, so the time is O(R * C) for R rows and C columns. It can stop earlier when a match is found. The extra space is constant, since only two loop counters are used and the result has two numbers.
Edge cases and common mistakes
- Several matches. The first in reading order wins:
[[5, 1], [1, 5]]with target 1 gives[0, 1], not[1, 0]. - Swapped order. The row comes first in the answer. For
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]with target 6,[1, 2]is correct and[2, 1]would point at 8. - Using
break. After abreakthe function would continue searching the later rows, and could report a later match or[-1, -1]. - Corners. The first cell,
[0, 0], and the last cell,[2, 2]in a 3 by 3 grid, are found correctly. - All cells equal to the target.
[[4, 4], [4, 4]]with target 4 returns[0, 0].
Testing
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]withtarget = 6gives[1, 2].- The same grid with
target = 1gives[0, 0], and withtarget = 9gives[2, 2]. [[5, 1], [1, 5]]withtarget = 1gives[0, 1].[[4, 4], [4, 4]]withtarget = 3gives[-1, -1].[[4, 4], [4, 4]]withtarget = 4gives[0, 0].
