Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/566.html
566. Reshape the Matrix (Easy)
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Example 1:
Input: nums = [[1,2], [3,4]] r = 1, c = 4 Output: [[1,2,3,4]] Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
Example 2:
Input: nums = [[1,2], [3,4]] r = 2, c = 4 Output: [[1,2], [3,4]] Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
Note:
- The height and width of the given matrix is in range [1, 100].
- The given r and c are all positive.
Solution 1.
-
class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { int originalRows = nums.length, originalColumns = nums[0].length; if (originalRows * originalColumns != r * c) return nums; int[][] reshape = new int[r][c]; int total = originalRows * originalColumns; for (int i = 0; i < total; i++) { int originalRow = i / originalColumns, originalColumn = i % originalColumns; int currentRow = i / c, currentColumn = i % c; reshape[currentRow][currentColumn] = nums[originalRow][originalColumn]; } return reshape; } } ############ class Solution { public int[][] matrixReshape(int[][] mat, int r, int c) { int m = mat.length, n = mat[0].length; if (m * n != r * c) { return mat; } int[][] ans = new int[r][c]; for (int i = 0; i < m * n; ++i) { ans[i / c][i % c] = mat[i / n][i % n]; } return ans; } }
-
// OJ: https://leetcode.com/problems/reshape-the-matrix/ // Time: O(MN) // Space: O(1) class Solution { public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int M = nums.size(), N = nums[0].size(); if (M * N != r * c) return nums; vector<vector<int>> ans; for (int i = 0; i < r; ++i) { vector<int> row; for (int j = 0; j < c; ++j) { int index = i * c + j; row.push_back(nums[index / N][index % N]); } ans.push_back(row); } return ans; } };
-
class Solution: def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]: m, n = len(nums), len(nums[0]) if m * n != r * c: return nums res = [[0] * c for _ in range(r)] for x in range(m * n): res[x // c][x % c] = nums[x // n][x % n] return res ############ class Solution(object): def matrixReshape(self, nums, r, c): """ :type nums: List[List[int]] :type r: int :type c: int :rtype: List[List[int]] """ if r * c != len(nums) * len(nums[0]): return nums m = len(nums) n = len(nums[0]) ans = [[0] * c for _ in range(r)] for i in range(r * c): ans[i / c][i % c] = nums[i / n][i % n] return ans
-
func matrixReshape(mat [][]int, r int, c int) [][]int { m, n := len(mat), len(mat[0]) if m*n != r*c { return mat } ans := make([][]int, r) for i := range ans { ans[i] = make([]int, c) } for i := 0; i < m*n; i++ { ans[i/c][i%c] = mat[i/n][i%n] } return ans }
-
function matrixReshape(mat: number[][], r: number, c: number): number[][] { let m = mat.length, n = mat[0].length; if (m * n != r * c) return mat; let ans = Array.from({ length: r }, v => new Array(c).fill(0)); let k = 0; for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { ans[Math.floor(k / c)][k % c] = mat[i][j]; ++k; } } return ans; }
-
impl Solution { pub fn matrix_reshape(mat: Vec<Vec<i32>>, r: i32, c: i32) -> Vec<Vec<i32>> { let r = r as usize; let c = c as usize; let m = mat.len(); let n = mat[0].len(); if m * n != r * c { return mat; } let mut i = 0; let mut j = 0; (0..r) .into_iter() .map(|_| { (0..c) .into_iter() .map(|_| { let res = mat[i][j]; j += 1; if j == n { j = 0; i += 1; } res }) .collect() }) .collect() } }