[LeetCode] 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:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

利用queue, 如果lr*lc == r*c的话.

Code

class Solution:
    def reshapeMatrix(self, nums, r, c):
        lr, lc = len(nums), len(nums[0])
        if lr*lc != r*c: return nums
        ans, queue = [[0]*c for _ in range(c)], collections.deque()
        for i in range(lr):
            for j in range(lc):
                queue.append(nums[i][j])
        for i in range(r):
            for j in range(c):
                ans[i][j] = queue.popleft()
        return ans

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9499585.html

时间: 2024-10-29 04:29:38

[LeetCode] 566. Reshape the Matrix_Easy的相关文章

LeetCode 566. Reshape the Matrix (重塑矩阵)

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 rep

LeetCode - 566. Reshape the Matrix

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 rep

leetcode 566 Reshape the Matrix 重塑矩阵

参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int m=nums.size();//m为nums行数 int n=nums[0].size(); //n为num

566. Reshape the Matrix - LeetCode

Question 566. Reshape the Matrix Solution 题目大意:给一个二维数组,将这个二维数组转换为r行c列 思路:构造一个r行c列的二维数组,遍历给出二给数组nums,合理转换原数组和目标数组的行列转换. Java实现: public int[][] matrixReshape(int[][] nums, int r, int c) { int originalR = nums.length; int originalC = nums[0].length; if

566. Reshape the Matrix

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #323333; background-color: #f5f5f5 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px

566. Reshape the Matrix矩阵重排

[抄题]: 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

LeetCode第五天

leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { int[][] newNums = new int[r][c]; int size = nums.length*nums[0].length; if(r*c != size) return nums; for(int i=0;i<siz

LeetCode Algorithms Questions List (LeetCode 算法题列表)- Java Solutions

因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解,链多了会眼花.  # Title Acceptance Difficulty  617  Merge Two Binary  69.9%  Easy  606  Construct String from Binary Tree  52.0%  Easy  599  Minimum Index Su

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea