1. 程式人生 > >leetcode 566 Reshape the Matrix 重塑矩陣

leetcode 566 Reshape the Matrix 重塑矩陣

shape HA 方法 i++ 復習 vector 目標 spa turn

參考: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為nums列數
        vector<vector<int
>> res(r, vector<int>(c)); //講真這個定義不太搞懂?? if(m*n!=r*c) return nums; for(int i=0;i<r;i++) //目標是res, 所以要按照r c循環打印。 for(int j=0;j<c;j++) { int k=c*i+j; //拉直 res[i][j]=nums[k/n][k%n]; //取行數(除以行數n取整),列數(取余)
} return res; } };

leetcode 566 Reshape the Matrix 重塑矩陣