Implementing 2D Array Algorithms
แนะนำใหม่
13:31
การเก็บรวบรวมข้อมูล
ถ่ายรูปหนึ่งรูปบนโทรศัพท์ของคุณ. สำหรับคอมพิวเตอร์แล้วมันไม่ใช่รูปภาพเลย — มันคือตารางของตัวเลข, ตัวเลขหนึ่งสำหรับทุกพิกเซล, ประมาณสิบสองล้านตัว. ลองทำ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| 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-D algorithms combine the array patterns with nested-loop traversal.
- Sum / count / max ทั้งกริด: รวมผลภายในลูปภายใน
- Search กริด: คืนค่า
[row][col]ที่พบ (หรือสัญญาณ "not found") - Logic accumulator/max เดิม, ตอนนี้ทำกับ
rows × columnsเซลล์
ผลรวมแถวและคอลัมน์
- แถวเดียว: ยึด
r, ลูปcตามคอลัมน์, รวมผลg[r][c] - คอลัมน์เดียว: ยึด
c, ลูปrตามแถว, รวมผลg[r][c] - การเลือกว่าจะยึดดัชนีไหนและลูปดัชนีไหนคือการตัดสินใจที่สำคัญ
- Row sum sweep across; column sum sweep down

Row-major และ column-major traversal ของ array 2D

เส้นทแยงมุม
- Main diagonal ของกริดสี่เหลี่ยมจัตุรัสคือเซลล์ที่มี row == column
- ลูปดัชนีหนึ่งตัว:
for (int i = 0; i < g.length; i++) { ... g[i][i] ... } g[0][0], g[1][1], g[2][2], …— ลูปเดียว, ทั้งสองดัชนีเท่ากัน- มีประโยชน์สำหรับกริดสี่เหลี่ยมจัตุรัส (ตรวจสอบ identity, เส้นทแยงมุมกระดาน)
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
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.
ตัดสินใจว่าจะยึดดัชนีไหนและลูปดัชนีไหน — นั่นคือความแตกต่างระหว่าง row sum กับ column sum. Row sum ยึด r และลูป c (g[r][c] Across); column sum ยึด c และลูป r (g[r][c] Down). และการแก้ไขเซลล์ต้องใช้ indexed nested loops (g[r][c] = …); for-each form สามารถอ่านเท่านั้น
รวมผลคอลัมน์ 0 ของกริด:
int sum = 0;for (int r = 0; r < g.length; r++) { sum += g[r][0]; }- ยึดคอลัมน์
0, ลูปลงตามแถว — เป็น column sum
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).
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.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้