Problem overview
The method rowSums receives grid, a list of rows where each row is a list of integers, and returns a list with the sum of each row, in the same order as the rows. Every row has the same number of values.
For [[1, 2, 3], [4, 5, 6]] the result is [6, 15]: row 0 adds up to 6 and row 1 to 15. For [[-1, -2], [-3, -4], [5, 5]] the result is [-3, -7, 10], one sum per row, top to bottom. For [[3], [1], [4]] each row has one value, so the result is [3, 1, 4].
Approach
This is the grid-sum idea with one change: the sum is not one number for the whole grid but one number per row. So the running total must start again at 0 for every row, and each finished total is saved in a result list before the next row begins.
That fixes the shape of the code. The outer loop visits rows. Inside it, a fresh total starts at 0, an inner loop adds up the row's values, and when the inner loop ends, the total is appended to the result.
Step by step
- Start an empty list named
sums. It collects one sum per row. - Loop
rowovergrid. The loop runs once per row, so it produces one sum per row. - Create
total = 0inside the loop. Because it sits inside, it restarts for every row, and one row's numbers never mix into the next row's sum. - Loop
valueoverrowinside the outer loop. It visits only the current row. - Add
valuetototalin the inner loop. - Append
totaltosumsafter the inner loop, still inside the outer one. At that point the whole row has been added, and appending at the end of each pass keeps the sums in row order. - Return
sumsafter the outer loop.
The solution in Python
class Solution:
def rowSums(self, grid: List[List[int]]) -> List[int]:
sums = []
for row in grid:
total = 0
for value in row:
total += value
sums.append(total)
return sums
Python details:
- Indentation decides which loop a line belongs to.
sums.append(total)lines up with the innerfor, so it runs once per row. Indented one level deeper, it would run once per value. appendchanges the list in place and returnsNone, so don't assign its result back tosums.- The name
sumsis free to use. Python has a built-insum, butsumsis a different name and hides nothing.
The built-in gives a shorter version: [sum(r) for r in grid] returns [6, 15] for the first example. The explicit loops show where each number comes from.
Edge cases and common mistakes
- Creating
totalbefore the outer loop. Then it never resets, and each sum includes all earlier rows. For the first example the result would be[6, 21]instead of[6, 15]. - Appending inside the inner loop. That adds a partial sum after every value, so the list grows by one item per cell instead of one per row.
- Returning inside the outer loop. The result is a list holding only the first row's sum,
[6]for the first example. - Rows with one value. They need no special case: the inner loop runs once and the sum is that value.
- Negative values. They work normally, and a row of negatives gives a negative sum.
Testing
[[1, 2, 3], [4, 5, 6]]gives[6, 15].[[-1, -2], [-3, -4], [5, 5]]gives[-3, -7, 10].[[3], [1], [4]]gives[3, 1, 4].
