7ms easy Java solution


  • 0
    C

    public class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {

        if(nums==null) return null;
        if(r*c!=nums.length*nums[0].length)return nums;
        if(r==nums.length)return nums;
        
        int[][] res= new int[r][c];
        int a=0;
        int b=0;
        for(int[] i:nums){
            for(int j:i)
            {
                res[a][b]=j;
                b++;
                if(b==c){
                    b=0;
                    a++;
                }
            }
            
        }
        return res;
    }
    

    }


Log in to reply
 

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