1. 程式人生 > >Reshape the Matrix---LeetCode566

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;