Reshape the Matrix---LeetCode566

题目描述

In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.

You’re given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the ‘reshape’ operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

Input:
nums = [[1,2], [3,4]]
r = 1, c = 4
Output: [[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

题目翻译过来就是:给定一个二维数组,然后给定两个正整数r和c想要改变数组为r行,c列,如果原数组可以被重塑,那么输出重塑后的数组;如果不可以重塑,那么输出原数组。

解法一

分析:
1)只用原数组的个数与需要的重塑的数组个数一样时,原数组才可以被重塑;
2)方法一采用了一种时间复杂度为O(n)的方法,将二维数组看成一维数组,那么元素的个数等于r*c,而第i个元素在原数组中的位置是[i/column][i%column],在新数组的位置是[i/c][i%c];
3)当然这里首先还是需要检验原数组是否为空

public int[][] matrixReshape(int[][] nums,int r,int c){
        if(nums==null||nums.length==0||(nums.length==1&&nums[0].length==0)||nums.length*nums[0].length!=r*c)
            return nums;
        int column,row;
        row=nums.length;
        column=nums[0].length;
        int[][] result=new int[r][c];
        for(int i=0;i<r*c;i++){
            result[i/c][i%c]=nums[i/column][i%column];
        }
        return result;
    }

解法二

分析:
1)方法二引入队列,先遍历原数组,将数组的元素放入队列中,然后在建立新数组时,再从队列中取出该元素,时间复杂度为O(m*n)

 public int[][] matrixReshape(int[][] nums, int r, int c) {        
        if(nums==null||nums.length==0||(nums.length==1&&nums[0].length==0)||nums.length*nums[0].length!=r*c)
            return nums;
        Queue<Integer> queue=new LinkedList<>();
        int[][] result=new int[r][c];
        for(int i=0;i<nums.length;i++){
            for(int j=0;j<nums[0].length;j++){
                queue.add(nums[i][j]);
            }
        }
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                result[i][j]=queue.remove();
            }
        }
        return result;

    }

解法三

分析:
1)方法三不引入变量,直接利用数组本身,但时间复杂度仍为O(m*n)

 public int[][] matrixReshape(int[][] nums, int r, int c) {        
        if(nums==null||nums.length==0||(nums.length==1&&nums[0].length==0)||nums.length*nums[0].length!=r*c)
            return nums;
        int row=0,cols=0;
        int[][] result=new int[r][c];
        for(int i=0;i<nums.length;i++){
            for(int j=0;j<nums[0].length;j++){
                result[row][cols]=nums[i][j];
                cols++;
                if(cols==c){
                    cols=0;
                    row++;
                }
            }
        }
        return result;

    }

这是解法一,引入row和cols,cols从0开始,逐渐加一,直到等于c时,转入下一行,然后row++;当然for循环部分也可以写成下面这种形式:

for(int i=0;i<nums.length;i++){
            for(int j=0;j<nums[0].length;j++){
                result[count/c][count%c]=nums[i][j];
                count++;
            }
        }

这里个人认为,引入一个count,然后重塑后的数组为result[count/c][count%c]与解法一思想类似

补充

Java判断二维数组是否为空:
int[][] arr; arr == null
int[][] arr = {}; arr.length == 0
int[][] arr = {{}}; arr.length == 1 && arr[0].length == 0
int[][] arr = {{}, {}}; arr[0].length == 0 && arr[1].length == 0
一是数组首地址是否为空
二是是否为{},也就是array.length==0的情况
三是{{}},这时array.length=1,但是array[0].length==0
总结为:if(array==null||array.length==0||(array.length==1&&array[0].length==0)) return false;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值