Đề bài
Return the sum of each row of the grid, in row order.
Implement row_sums(grid).
Input
grid: A list of rows of integers.
Output
Return one total per row.
Ràng buộc
0 ≤ rows ≤ 1,000; 0 ≤ values per row ≤ 1,000
Ví dụ
Đầu vào:
grid = [
[1, 2, 3],
[4, 5, 6]
]
Kết quả mong đợi:
expected = [6, 15]
Đầu vào:
std::vector<std::vector<long long>> grid = {
{1, 2, 3},
{4, 5, 6}
};
Kết quả mong đợi:
std::vector<long long> expected = {6, 15};
Đầu vào:
long[][] grid = new long[][] {
new long[] {1, 2, 3},
new long[] {4, 5, 6}
};
Kết quả mong đợi:
long[] expected = new long[] {6, 15};
Giải thích
1+2+3 = 6 and 4+5+6 = 15: [6, 15].
Đầu vào:
grid = [
[],
[7]
]
Kết quả mong đợi:
expected = [0, 7]
Đầu vào:
std::vector<std::vector<long long>> grid = {
{},
{7}
};
Kết quả mong đợi:
std::vector<long long> expected = {0, 7};
Đầu vào:
long[][] grid = new long[][] {
new long[] {},
new long[] {7}
};
Kết quả mong đợi:
long[] expected = new long[] {0, 7};
Giải thích
An empty row adds up to 0: [0, 7].
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Each output value comes from one row of the grid.
Gợi ý 2
Sum the numbers in each row independently and keep the results in order.
Gợi ý 3
An empty grid should produce an empty result.
Độ phức tạp mong đợi
O(total values) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.