Implementing 2D Array Algorithms · 实现二维数组算法
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| nested-loop/ˈnestɪd luːp/ | 嵌套循环 | qiàn tào xún huán |
| main diagonal/meɪn daɪˈæɡənl/ | 主对角线 | zhǔ duì jiǎo xiàn |
| indexed/ˈɪndekst/ | 带下标 | dài xià biāo |
Grid algorithms
- 2-D algorithms combine the array patterns with nested-loop 嵌套循环 traversal.
- Sum / count / max over the whole grid: accumulate inside the inner loop.
- Search a grid: return the
[row][col]where found (or a "not found" signal). - The same accumulator/max logic, now over
rows × columnscells.
网格算法
- 二维算法把数组模式与嵌套循环遍历组合起来。
- 对整个网格求和 / 计数 / 最大:在内层循环里累加。
- 搜索网格:返回找到处的
[row][col](或一个“未找到”信号)。 - 同样的累加器/最大逻辑,现在遍及
行 × 列个单元格。
Row and column sums
- One row: fix
r, loopcover the columns, summingg[r][c]. - One column: fix
c, looprover the rows, summingg[r][c]. - Choosing which index to fix and which to loop is the key decision.
- A row sum sweeps across; a column sum sweeps down.
行和与列和
- **一行:**固定
r,让c遍历各列,累加g[r][c]。 - **一列:**固定
c,让r遍历各行,累加g[r][c]。 - 选择固定哪个下标、遍历哪个,是关键决定。
- 行和横向扫过;列和纵向扫下。
The diagonal
- The main diagonal 主对角线 of a square grid is the cells where row == column.
- Loop one index:
for (int i = 0; i < g.length; i++) { ... g[i][i] ... } g[0][0], g[1][1], g[2][2], …— a single loop, both indices equal.- Useful for square grids (identity checks, board diagonals).
对角线
- 方形网格的主对角线是行 == 列的那些单元格。
- 循环一个下标:
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] = .... - The for-each version can't write back to the grid.
- 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.
修改单元格
- 要改变一个单元格,用带下标的嵌套循环并赋值
g[r][c] = ...。 - for-each 版本不能写回网格。
- 留意两个边界——错误的长度会读或写错单元格。
- 仔细追踪
[r][c],尤其当行列数不同时。
Decide which index to fix and which to loop — that's the difference between a row sum and a column sum. A row sum fixes r and loops c (g[r][c] across); a column sum fixes c and loops r (g[r][c] down). And modifying cells needs the indexed nested loops (g[r][c] = …); the for-each form can only read.
决定固定哪个下标、遍历哪个——这就是行和与列和的区别。一个行和固定 r 并遍历 c(g[r][c] 横向);一个列和固定 c 并遍历 r(g[r][c] 纵向)。而修改单元格需要带下标的嵌套循环(g[r][c] = …);for-each 形式只能读。
Summing column 0 of a grid:
int sum = 0;for (int r = 0; r < g.length; r++) { sum += g[r][0]; }- Fixes column
0, loops down the rows — a column sum.
对网格的第 0 列求和:
int sum = 0;for (int r = 0; r < g.length; r++) { sum += g[r][0]; }- 固定第
0列,沿行往下循环——一个列和。
2-D algorithms apply array patterns over nested loops: whole-grid sum/count/max/search, a row sum (fix r, loop c), a column sum (fix c, loop r), or the diagonal (g[i][i]). Modifying cells needs the indexed loops (g[r][c] = …); mind both bounds (g.length rows, g[0].length columns).
二维算法在嵌套循环上应用数组模式:整网格的求和/计数/最大/搜索、一个行和(固定 r,遍历 c)、一个列和(固定 c,遍历 r),或对角线(g[i][i])。修改单元格需要带下标循环(g[r][c] = …);留意两个边界(g.length 行,g[0].length 列)。
Summing column 0 · 对第 0 列求和
g[0][0]=1, g[1][0]=4, g[2][0]=7: running sum 1, 5, 12. · g[0][0]=1、g[1][0]=4、g[2][0]=7:累加 1、5、12。
To sum ONE row r, you... · 要对某一行 r 求和,你……
A row sum fixes the row, loops the columns. · 行和固定行,遍历列。
To sum ONE column c, you... · 要对某一列 c 求和,你……
A column sum fixes the column, loops the rows. · 列和固定列,遍历行。
The main diagonal of a square grid is the cells where... · 方形网格的主对角线是……的单元格。
g[0][0], g[1][1], g[2][2] — one index, both equal. · g[0][0]、g[1][1]、g[2][2]——一个下标,两者相等。
The for-each form can modify (write to) grid cells. · for-each 形式可以修改(写入)网格单元格。
For-each is read-only; use indexed loops (g[r][c] = ...) to write. · for-each 只读;用带下标循环(g[r][c] = ...)来写。
Column 0 of {{1,2},{4,5},{7,8}} is 1, 4, 7. What is its sum? · {{1,2},{4,5},{7,8}} 的第 0 列是 1、4、7。它的和是多少?
1 + 4 + 7 = 12. · 1 + 4 + 7 = 12。
Order the steps to sum column c. · 给对第 c 列求和的步骤排序。
Initialise, loop the rows, accumulate, return. · 初始化、遍历行、累加、返回。