Implementing 2D Array Algorithms · Implementando Algoritmos de Array 2D
| English | Português |
|---|---|
| nested-loop/ˈnestɪd luːp/ | loop aninhado |
| main diagonal/meɪn daɪˈæɡənl/ | diagonal principal |
| indexed/ˈɪndekst/ | indexado |
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.
Algoritmos de grade
- Algoritmos 2-D combinam os padrões de array com travessia de loops aninhados 嵌套循环.
- Soma / contagem / máximo sobre toda a grade: acumule dentro do loop interno.
- Pesquisa em uma grade: retorne o
[row][col]onde encontrado (ou um sinal de "não encontrado"). - A mesma lógica de acumulador/máximo, agora sobre
rows × columnscélulas.
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.
Somas de linha e coluna
- Uma linha: fixe
r, faça loopcsobre as colunas, somandog[r][c]. - Uma coluna: fixe
c, faça looprsobre as linhas, somandog[r][c]. - Escolher qual índice fixar e qual percorrer é a decisão-chave.
- Uma soma de linha varre horizontalmente; uma soma de coluna varre verticalmente.
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).
A diagonal
- A diagonal principal 主对角线 de uma grade quadrada são as células onde row == column.
- Faça loop em um índice:
for (int i = 0; i < g.length; i++) { ... g[i][i] ... } g[0][0], g[1][1], g[2][2], …— um loop único, ambos os índices iguais.- Útil para grades quadradas (verificações de identidade, diagonais de tabuleiro).
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.
Modificando células
- Para alterar uma célula, use os loops aninhados 带下标 indexados e atribua
g[r][c] = .... - A versão for-each não pode gravar de volta na grade.
- Observe os dois limites — um comprimento errado lê ou grava a célula errada.
- Rastreie
[r][c]cuidadosamente, especialmente quando linhas e colunas diferem na contagem.
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.
Decida qual índice fixar e qual percorrer — essa é a diferença entre uma soma de linha e uma soma de coluna. Uma soma de linha fixa r e faz loop c (g[r][c] horizontalmente); uma soma de coluna fixa c e faz loop r (g[r][c] verticalmente). E modificar células precisa dos loops aninhados indexados (g[r][c] = …); a forma for-each só pode ler.
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.
Somando a coluna 0 de uma grade:
int sum = 0;for (int r = 0; r < g.length; r++) { sum += g[r][0]; }- Fixa a coluna
0, faz loop verticalmente pelas linhas — uma soma de coluna.
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).
Algoritmos 2-D aplicam padrões de array sobre loops aninhados: soma/contagem/máximo/pesquisa de grade inteira, uma soma de linha (fixe r, faça loop c), uma soma de coluna (fixe c, faça loop r), ou a diagonal (g[i][i]). Modificar células precisa dos loops indexados (g[r][c] = …); observe ambos os limites (g.length linhas, g[0].length colunas).
Summing column 0 · Somando a coluna 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: soma acumulativa 1, 5, 12.
To sum ONE row r, you... · Para somar UMA linha r, você...
A row sum fixes the row, loops the columns. · Uma soma de linha fixa a linha, faz loop nas colunas.
To sum ONE column c, you... · Para somar UMA coluna c, você...
A column sum fixes the column, loops the rows. · Uma soma de coluna fixa a coluna, faz loop nas linhas.
The main diagonal of a square grid is the cells where... · A diagonal principal de uma grade quadrada são as células onde...
g[0][0], g[1][1], g[2][2] — one index, both equal. · g[0][0], g[1][1], g[2][2] — um índice, ambos iguais.
The for-each form can modify (write to) grid cells. · A forma for-each pode modificar (escrever em) células de grid.
For-each is read-only; use indexed loops (g[r][c] = ...) to write. · For-each é read-only; use loops indexados (g[r][c] = ...) para escrever.
Column 0 of {{1,2},{4,5},{7,8}} is 1, 4, 7. What is its sum? · Coluna 0 de {{1,2},{4,5},{7,8}} é 1, 4, 7. Qual é sua soma?
1 + 4 + 7 = 12.
Order the steps to sum column c. · Organize os passos para somar a coluna c.
Initialise, loop the rows, accumulate, return. · Inicialize, faça loop nas linhas, acumule, retorne.