566. Reshape the Matrix Easy

@problem@discussion
#Array#Matrix#Simulation



1/**
2 * [566] Reshape the Matrix
3 *
4 * In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.
5 * You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.
6 * The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
7 * If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/reshape1-grid.jpg" style="width: 613px; height: 173px;" />
11 * Input: mat = [[1,2],[3,4]], r = 1, c = 4
12 * Output: [[1,2,3,4]]
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/reshape2-grid.jpg" style="width: 453px; height: 173px;" />
16 * Input: mat = [[1,2],[3,4]], r = 2, c = 4
17 * Output: [[1,2],[3,4]]
18 * 
19 *  
20 * Constraints:
21 * 
22 * 	m == mat.length
23 * 	n == mat[i].length
24 * 	1 <= m, n <= 100
25 * 	-1000 <= mat[i][j] <= 1000
26 * 	1 <= r, c <= 300
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/reshape-the-matrix/
32// discuss: https://leetcode.com/problems/reshape-the-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn matrix_reshape(mat: Vec<Vec<i32>>, r: i32, c: i32) -> Vec<Vec<i32>> {
38        vec![]
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_566() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.