problem
solution
class Solution { public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int nums_r = nums.size(); int nums_c = nums[0].size(); if(nums_r*nums_c != r*c) return nums; vector<vector<int>> ans(r, vector<int>(c));//err. for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { int k = i * c + j;// ans[i][j] = nums[k / nums_c][k % nums_c];//err. } } return ans; } };
solution2
class Solution { public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int nums_r = nums.size(); int nums_c = nums[0].size(); if(nums_r*nums_c != r*c) return nums; vector<vector<int>> ans(r, vector<int>(c));//err. for(int i=0; i<r*c; i++) { ans[i / c][i % c] = nums[i / nums_c][i % nums_c]; } return ans; } };
re
1. Leetcode_easy_566. Reshape the Matrix;
2. Grandyang;
end