Matrix
Classical Questions and Solutions for Matrix
Matrix Manipulation
Example questions:
311_Sparse Matrix Multiplication
Search in Matrix
Start from top-right, eliminate one row or one column a time.
Example questions:
240_Search a 2D Matrix II
Traverse in Matrix
There are various traverse types. But we can always learn from examples.
Need to take care of the direction, and corner cases on each border.
Sometimes we could use sorting.
Useful facts: for elements on the same row, their row index are the same; for elements on the same column, their column index are the same; for elements on the same diagonal, the sum of row index and column index are the same.
Example questions:
54_Spiral Matrix
498_Diagonal Traverse
Notes:
Construct 2D array/matrix
When construct 2*3 matrix of zeros, don't write
[[0] * 3 ] * 2
, the correct way is[[0] * 3 for _ in range(2)]
. Although it look like that they return the same matrix. A big difference is the first code generate all rows referring to the first row. Thus when one row changes, every row changes. The second code doesn't have this reference problem.
Last updated