DevPath

← All problems

Row Sums

Easy matrix

Description

Return a list with the sum of each row.

Constraints

1 <= rows, cols <= 1000

Examples

Input: matrix = [[1,2],[3,4],[5,6]]

Output: [3,7,11]

Hints (1)
  • Sum each row.

Solutions

Sum each row. · Time O(rows*cols) · Space O(rows)

func rowSums(matrix [][]int) []int {
    out := make([]int, len(matrix))
    for i, row := range matrix {
        sum := 0
        for _, v := range row {
            sum += v
        }
        out[i] = sum
    }
    return out
}

Create a free account to submit solutions, track progress and save problems.

Create account