[]566. Reshape the Matrix

problem

566. Reshape the Matrix

 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

posted on 2019-06-03 22:07  鹅要长大  阅读(125)  评论(0编辑  收藏  举报

导航