Problem overview
RowSums receives grid, an int[][], and returns a List<int> holding the sum of each row, in the same order as the rows. A grid is a table: every row has the same number of values, one per column, and rows are counted from the top starting at 0.
For the rows [1, 2, 3] and [4, 5, 6] the result is [6, 15]. For [-1, -2], [-3, -4] and [5, 5] it is [-3, -7, 10]: a row of negative numbers has a negative sum. For three rows with one value each, [3], [1] and [4], each sum is just that value, so the result is [3, 1, 4].
Approach: reset the total for every row
Summing a whole grid needs one total. Here the answer has one sum per row, so each row gets its own total. The shape of the code is:
- an outer loop over the rows;
- inside it, a fresh
totalthat starts at 0; - an inner loop that adds the row's values to that total;
- after the inner loop, the finished total goes into the result list.
Where each of these lines sits is the whole lesson. Create total inside the outer loop and it starts at 0 again for every row, so one row's numbers never leak into the next row's sum. Add it to the list at the end of the outer loop's body, and the sums come out in row order.
Step by step
- Create an empty
List<int>namedsums. The sums are added one at a time, so it starts with nothing in it. - Loop over the rows with
foreach (int[] row in grid). Each item of anint[][]is anint[], so that is the loop variable's type. The loop runs once per row, and each row gives exactly one sum. - Create
int total = 0inside this loop. It holds the sum of the current row only. - Write the inner loop,
foreach (int value in row). It visits the values of the current row and only that row. - Add
valuetototalinside the inner loop. - After the inner loop, still inside the outer one, call
sums.Add(total). When the inner loop has ended, the whole row has been added, sototalis this row's sum. - Return
sumsafter the outer loop. Returning inside it would hand back only the first row's sum.
The solution in C#
class Solution {
public List<int> RowSums(int[][] grid) {
List<int> sums = new List<int>();
foreach (int[] row in grid) {
int total = 0;
foreach (int value in row) {
total += value;
}
sums.Add(total);
}
return sums;
}
}
C# details:
- A list, not an array. The result is a
List<int>because it is built by adding items. An array's length is fixed when it is created, while a list grows withAdd. For a list, useCountfor its size; for an array,Length. - Scope. A variable declared inside a loop body, like
totalhere, exists only for that pass of the loop. That is exactly what the lesson wants: every pass has its owntotal. - Printing a list.
Console.WriteLineon aList<int>prints the type's name, not the numbers. To see the contents, usestring.Join(", ", sums). For the first example it gives6, 15. List<T>is inSystem.Collections.Generic; in a file of your own, addusing System.Collections.Generic;.
Edge cases and common mistakes
- Declaring
totalbefore the outer loop. It would never reset, so every sum would include all the rows above it. For the first example the result would be[6, 21]instead of[6, 15]. - Adding to
sumsinside the inner loop. The list would get one item per value, not per row. - Adding to
sumsbefore the inner loop. It would add the row's sum before it is computed, which is always 0. - Single-column grids. Each row has one value, so every sum equals that value, and the list has as many items as the grid has rows.
- Negative values. They need no special handling.
Testing
grid | Result |
|---|---|
[[1, 2, 3], [4, 5, 6]] | [6, 15] |
[[-1, -2], [-3, -4], [5, 5]] | [-3, -7, 10] |
[[3], [1], [4]] | [3, 1, 4] |
