leetcode算法: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 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 = 4Output: [[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 = 4Output: [[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. 

这道题描述的需求是:需要我们重构一个矩阵提供给我们某一个矩阵,然后再给我们两个整数r和c 表示希望我们重构成r 行 c 列 的矩阵如果不能重构返回原行列式

思想就是:首先要判断一下 如果r*c等于给定矩阵的元素数量,那就能重构  重构 把所有元素取出来 再按照要求生成新的矩阵就可以了

我的python代码:
 1 class Solution(object):
 2     def matrixReshape(self, nums, r, c):
 3         """
 4         :type nums: List[List[int]]
 5         :type r: int
 6         :type c: int
 7         :rtype: List[List[int]]
 8         """
 9         if r*c == len(nums)*len(nums[0]):
10             newList = [ i for j in range(len(nums) ) for i in nums[j]  ]
11             return  [ [ newList.pop(0) for i in range(c) ] for j in range(r) ]
12         else:
13             return nums
14
15
16
17 if __name__ == ‘__main__‘:
18     s = Solution()
19     res = s.matrixReshape([[1,2],[3,4]],1,4)
20     #res = s.matrixReshape([[1, 2], [3, 4]],4,1)
21     print(res)
				
时间: 2024-08-06 22:04:27

leetcode算法:Reshape the Matrix的相关文章

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

[LeetCode] 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题解)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] 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

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

leetcode第一刷_Spiral Matrix

我觉得这个题好无聊啊,好端端一个数组,干嘛要跟比巴卜一样转一圈输出呢.. 思想很简单,每次从左到右,再从上到下,在从右到左,再从下到上.问题是每次到什么时候该改变方向.我的做法是用一个变量保存当前在第几层,这个层是相对于从外向内有几圈来说的.注意想清楚边界的话这个题一点也不难.有个细节,我的循环退出条件是访问的数跟矩阵总个数之间的关系,如果有一次在判断进入循环是条件是满足的,但是在循环内部不满足了,我的策略是忽略这种情况,虽然这样会在结果集中多加一些重复的数据,但是以我的算法,一定是先访问没有访

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