Array-leetcode 566 Reshape the Matrix

来源:互联网 发布:查询域名是否已备案 编辑:程序博客网 时间:2024/06/03 22:52

原题链接:Reshape the Matrix


题解:

public class Solution {    public int[][] matrixReshape(int[][] nums, int r, int c) {        /*            Time Complexity:O(M*N)            Space Complexity:O(M*N)        */        int[][]res=new int[r][c];        int x=0,y=0;        if(nums.length==0 || r*c!=nums[0].length*nums.length)return nums;        for(int i=0;i<nums.length;i++)        {            for(int j=0;j<nums[0].length;j++){                res[x][y]=nums[i][j];                y++;                if(y==c){                    x++;                    y=0;                }            }        }        return res;            }}