Implementing 2D Array Algorithms
導入済み
13:31
データコレクション
Take one photo on your phone. To the computer it is not a picture at all — it is a grid of numbers, one for every pixel, about twelve million of them. Now try…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| nested-loop/ˈnestɪd luːp/ | ネストループ |
| column sum | |
| 2D array/ˌtuː ˈdiː əˈreɪ/ | In Java, an array whose elements refer to arrays; rows can have different lengths, so it need not form a rectangular grid. |
| main diagonal/meɪn daɪˈæɡənl/ | 対角成分 |
| indexed/ˈɪndekst/ | 添字付き |
グリッドアルゴリズム
- 2次元アルゴリズムは、配列のパターンと 重複ループ による走査を組み合わせます。
- グリッド全体の 合計 / カウント / 最大値: 内側ループ内で蓄積します。
- グリッドの 検索: 見つかった位置の
[row][col]を返す(または「未発見」信号)。 - 同じ蓄積器/最大値ロジックを、
rows × columnsセル全体に対して適用します。
行と列の合計
- 1行:
rを固定し、列に対してcをループしてg[r][c]を合計します。 - 1つの列:
cを固定し、行に対してrをループしてg[r][c]を合計します。 - どのインデックスを固定し、どのインデックスをループするかを選ぶことが重要な判断です。
- 行の合計は横方向にスウィープし、列の合計は縦方向にスウィープします。

Row-majorおよびcolumn-majorによる2D配列の走査

対角線
- 正方形グリッドの メイン対角線 は、行 == 列 のセルです。
- インデックス1つでループ:
for (int i = 0; i < g.length; i++) { ... g[i][i] ... } g[0][0], g[1][1], g[2][2], …— 単一のループで、両方のインデックスが等しくなります。- 正方形グリッドに有用です(恒等チェック、ボードの対角線など)。
このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
Modifying cells
- To change a cell, use the indexed 带下标 nested loops and assign
g[r][c] = .... - An enhanced loop over rows can write to cells using an index, such as
row[0] = 1; assigning to a primitive inner-loop variable does not change a cell. - Watch the two bounds — a wrong length reads or writes the wrong cell.
- Trace
[r][c]carefully, especially when rows and columns differ in count.
どのインデックスを固定し、どのインデックスをループするかを決定することが、行の和と列の和の違いです。 行の和は r を固定して c をループします (g[r][c] を横方向に)。 列の和は c を固定して r をループします (g[r][c] を縦方向に)。セルを修正するにはインデックス付きネストループ (g[r][c] = … ) が必要です; for-each 形式では読み取りのみ可能です。
グリッドの列0の合計計算:
int sum = 0;for (int r = 0; r < g.length; r++) { sum += g[r][0]; }- 列
0を固定し、行を下にループ — 列の合計です。
2次元アルゴリズムは、重複ループ 上で配列パターンを適用します:グリッド全体の 合計/カウント/最大値/検索、行 の合計(r 固定、c ループ)、列 の合計(c 固定、r ループ)、または 対角線 (g[i][i])。セルの変更には インデックス付き ループが必要です(g[r][c] = …);両方の境界を確認してください(g.length 行、g[0].length 列)。
Summing column 0
g[0][0]=1, g[1][0]=4, g[2][0]=7: running sum 1, 5, 12.
To sum ONE row r, you...
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A row sum fixes the row, loops the columns.
To sum ONE column c, you...
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A column sum fixes the column, loops the rows.
The main diagonal of a square grid is the cells where...
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
g[0][0], g[1][1], g[2][2] — one index, both equal.
Assigning a new value to the primitive inner-loop variable in a nested for-each loop changes the corresponding grid cell.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Assigning to the inner-loop variable does not change the cell; a row reference can still be used with an index to write a cell.
Column 0 of {{1,2},{4,5},{7,8}} is 1, 4, 7. What is its sum?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
1 + 4 + 7 = 12.
Order the steps to sum column c.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Initialise, loop the rows, accumulate, return.
何が問題でしたか?この内容はサイトの運営チームだけが確認します。