Array-leetcode 566 Reshape the Matrix

xiaoxiao2021-02-28  121

原题链接: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; } }

转载请注明原文地址: https://www.6miu.com/read-46464.html

最新回复(0)