Problem overview
Given a grid grid of type int[][], rowSums returns a list with the sum of each row, in the same order as the rows. The grid is a table of numbers where every row has the same number of values, and rows are counted from the top, starting at 0.
For [[1, 2, 3], [4, 5, 6]] the result is [6, 15]: row 0 adds up to 1 + 2 + 3 and row 1 to 4 + 5 + 6. For [[-1, -2], [-3, -4], [5, 5]] it is [-3, -7, 10]. For [[3], [1], [4]], where every row has a single value, it is [3, 1, 4].
Approach
Summing a whole grid needs one running total. Here the answer is a list of totals, one per row, so each row needs its own total that starts fresh. The pattern is:
- Go through the rows one at a time.
- For each row, start a total at 0 and add the row's values to it.
- When the row is finished, save its total in the result list.
Because the outer loop takes the rows from the top down and each total is saved as soon as its row ends, the sums come out in row order with no extra work.
Step by step
- Create
sums, an emptyList<Integer>backed by a newArrayList. The sums go in one at a time, so it starts empty. - Write a for-each loop with an
int[]namedrowovergrid. Each row gives exactly one sum, so the loop runs once per row. - Inside it, create an
intnamedtotalstarting at 0. Because it is created inside the outer loop, it starts again at 0 for every row, and one row's numbers never mix into the next row's sum. - Below that, still in the outer loop, write an inner for-each loop with an
intnamedvalueoverrow. It visits the values of the current row only. - Inside the inner loop, add
valuetototal. - After the inner loop but still inside the outer one, call
sums.add(total). At that point the whole row has been added, sototalis this row's sum. - After both loops, return
sums. Returning inside the outer loop would hand back only the first row's sum.
Tracing the second example
For [[-1, -2], [-3, -4], [5, 5]], the first row gives -3, the second -7 and the third 10. Each total is added to sums right after its row is done, and the method returns [-3, -7, 10].
The solution in Java
class Solution {
public List<Integer> rowSums(int[][] grid) {
List<Integer> sums = new ArrayList<>();
for (int[] row : grid) {
int total = 0;
for (int value : row) {
total += value;
}
sums.add(total);
}
return sums;
}
}
Java details in this code:
- Where a variable is declared decides its lifetime.
totalexists only inside the body of the outer loop. Each turn creates a new one, set to 0, and it disappears at the closing brace. That is exactly the "fresh total per row" behaviour the problem needs, without a line that resets it. List<Integer>, notList<int>. Collections hold objects, so the element type is the wrapperInteger. Java boxes theintautomatically insums.add(total).- Interface on the left. The variable is declared as
List, and the object created is anArrayList. The method's return type isList<Integer>as well. - The imports.
ListandArrayListlive injava.util, so a standalone program needsimport java.util.*;. - Printing. A list prints as
[6, 15], with square brackets and commas.
Edge cases and common mistakes
- Declaring
totaloutside the outer loop. The first row would add up correctly, but the second row's total would start from the first row's. For the first example you would get[6, 21]instead of[6, 15]. - Adding to
sumsinside the inner loop. That adds a running total after every value, so the list would have one entry per cell instead of one per row. - Adding to
sumsafter the outer loop. Only the last row's total would be saved, and only one number would come back. - Single-value rows. Each sum equals the value itself, as in
[[3], [1], [4]]. The same code handles this. - Negative values. They make a row's sum smaller, and can make it negative. Nothing special is needed.
Testing
rowSums(new int[][]{{1, 2, 3}, {4, 5, 6}})returns[6, 15].rowSums(new int[][]{{-1, -2}, {-3, -4}, {5, 5}})returns[-3, -7, 10].rowSums(new int[][]{{3}, {1}, {4}})returns[3, 1, 4].
