Java one loop solution

  • 0
    public class Solution {
        public int[][] matrixReshape(int[][] nums, int r, int c) {
            int m=nums.length;
            if(m==0)    return nums;
            int n=nums[0].length;
            if(m*n != r*c)  return nums;
            int[][] res = new int[r][c];
            for(int i=0; i<m*n; i++){
                res[i/c][i%c] = nums[i/n][i%n];
            return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.