Transpose a grid · 网格转置
Transpose a grid
Swap the rows and columns of a grid, as if you reflected a table across its diagonal.
grid[r][c] is the item in row r and column c. The new grid has one row for each column of the old one, so loop over the column numbers first, and inside that loop collect row[col] from every row.
网格转置
交换网格的行和列,就像沿着对角线翻转一张表格。
grid[r][c] 是第 r 行、第 c 列的元素。新网格的每一行对应旧网格的每一列,所以先遍历列号,再在这个循环里从每一行取出 row[col]。
Write transpose(grid) that turns rows into columns. grid is a list of rows, all the same length. [[1, 2, 3], [4, 5, 6]] becomes [[1, 4], [2, 5], [3, 6]]. · 编写 transpose(grid),把行变成列。grid 是由若干行组成的列表,每行长度相同。[[1, 2, 3], [4, 5, 6]] 变成 [[1, 4], [2, 5], [3, 6]]。
Click Run to see the output here. · 点击“运行”查看此处输出。