簡単 3件解決済み 正答率75.0%

Row Sums

Add up every row of a grid.

このチャレンジで学べること

  • Loop over the rows of a grid
  • Sum the values in each row
  • Return one total per row

問題

翻訳を表示

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.

制約

0 ≤ rows ≤ 1,000; 0 ≤ values per row ≤ 1,000

例

例 1
入力:
grid = [
    [1, 2, 3],
    [4, 5, 6]
]

期待される出力:
expected = [6, 15]
入力:
std::vector<std::vector<long long>> grid = {
    {1, 2, 3},
    {4, 5, 6}
};

期待される出力:
std::vector<long long> expected = {6, 15};
入力:
long[][] grid = new long[][] {
    new long[] {1, 2, 3},
    new long[] {4, 5, 6}
};

期待される出力:
long[] expected = new long[] {6, 15};

解説

1+2+3 = 6 and 4+5+6 = 15: [6, 15].

例 2
入力:
grid = [
    [],
    [7]
]

期待される出力:
expected = [0, 7]
入力:
std::vector<std::vector<long long>> grid = {
    {},
    {7}
};

期待される出力:
std::vector<long long> expected = {0, 7};
入力:
long[][] grid = new long[][] {
    new long[] {},
    new long[] {7}
};

期待される出力:
long[] expected = new long[] {0, 7};

解説

An empty row adds up to 0: [0, 7].

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Each output value comes from one row of the grid.

ヒント 2

Sum the numbers in each row independently and keep the results in order.

ヒント 3

An empty grid should produce an empty result.

目安の計算量

O(total values) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 HJhettory Jurash Python 674 ms - 4/5 コードを見る
2 LMLaura Mahlow Python 702 ms - 4/5 コードを見る
3 KKKaneki Ken Python 708 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が3件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。