原题地址(简单)
方法1—二维数组的一维表示
思路
就是找数学上的对应关系,直接看官方题解就可以了,很简单的。
方法2—按行遍历
思路
挨个遍历,更简单》。。。。
代码
class Solution {public:vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {vector<vector<int>> vv(r, vector<int>(c));int row = nums.size(), col = nums[0].size(), i = 0, j = 0;if(row * col != r * c) return nums;for(int x = 0; x < r; x++) {for(int y = 0; y < c; y++) {vv[x][y] = nums[i][j++];if(j >= col) {i++;j = 0;}}}return vv;}};
时空复杂度
时间 空间
返回数组空间不计算在内
