Leetcode - Arrays | Matrix
54. Spiral Matrix[M]
https://leetcode.com/problems/spiral-matrix/
Description
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
https://leetcode.com/problems/spiral-matrix/discuss/20571/1-liner-in-Python-%2B-Ruby
1 | class Solution: |
59. Spiral Matrix II[M]
https://leetcode.com/problems/spiral-matrix-ii/
Description
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
1 | Input: 3 |
Solution
73. Set Matrix Zeroes[M]
https://leetcode.com/problems/subarray-sum-equals-k/
Description
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Follow up:
- A straight forward solution using O(m**n) space is probably a bad idea.
- A simple improvement uses O(m + n) space, but still not the best solution.
- Could you devise a constant space solution?
Solution
https://leetcode.com/problems/set-matrix-zeroes/solution/
1 |
74. Search a 2D Matrix[M]
https://leetcode.com/problems/search-a-2d-matrix/
Description
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
1 |
85. Maximal Rectangle[H]
https://leetcode.com/problems/maximal-rectangle/
Description
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
Example:
1 | Input: |
Solution
https://discuss.leetcode.com/topic/6650/share-my-dp-solution/2
1 |
221. Maximal Square[M]
https://leetcode.com/problems/maximal-square/
Description
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.
Example:
1 | Input: |
Solution
https://leetcode.com/problems/maximal-square/solution/
1 |
766. Toeplitz Matrix[E]
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/
Description
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Note:
matrixwill be a 2D array of integers.matrixwill have a number of rows and columns in range[1, 20].matrix[i][j]will be integers in range[0, 99].
Follow up:
- What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
- What if the matrix is so large that you can only load up a partial row into the memory at once?
Solution
https://leetcode.com/problems/toeplitz-matrix/solution/
1 | class Solution: |
867. Transpose Matrix[M]
https://leetcode.com/problems/transpose-matrix/
Description
Given a matrix A, return the transpose of A.
The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix.

Example 1:
1 | Input: [[1,2,3],[4,5,6],[7,8,9]] |
Example 2:
1 | Input: [[1,2,3],[4,5,6]] |
Note:
1 <= A.length <= 10001 <= A[0].length <= 1000
Solution
https://leetcode.com/problems/transpose-matrix/solution/
1 |





