mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2026-10-30 04:14:08 +08:00
contents: add matrix index conversion tip
Document how to translate between flattened indices and matrix coordinates so readers can apply one-dimensional algorithms such as binary search to matrices. Closes #689
This commit is contained in:
@@ -50,6 +50,23 @@ Copying a matrix in Python is:
|
||||
copied_matrix = [row[:] for row in matrix]
|
||||
```
|
||||
|
||||
### Converting between 1D and 2D indices
|
||||
|
||||
An `m x n` matrix can be treated as a one-dimensional array with `m * n` elements. Given a zero-based index in that array, divide it by the number of columns to get the row and use the remainder to get the column:
|
||||
|
||||
```py
|
||||
row = index // num_columns
|
||||
col = index % num_columns
|
||||
```
|
||||
|
||||
To convert a row and column back into a one-dimensional index:
|
||||
|
||||
```py
|
||||
index = row * num_columns + col
|
||||
```
|
||||
|
||||
This technique can be useful when applying algorithms such as binary search to a matrix.
|
||||
|
||||
### Transposing a matrix
|
||||
|
||||
The transpose of a matrix is found by interchanging its rows into columns or columns into rows.
|
||||
|
||||
Reference in New Issue
Block a user