ReshapeTheMatrix [source code]

public class ReshapeTheMatrix {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        if ((nums.length == 0) || (nums.length > 100)) return nums;
        int[][] res = new int[r][c];
        int count = 0;          //1-dimension index of entry;
        for (int[] column : nums) {
            if ((column.length == 0) || (column.length > 100)) return nums; 
            for (int i : column) {
                if (count + 1 > r * c) return nums;         //return original if output matrix is too small; 
                res[count / c][count % c] = i;
                count++;
            }
        }
        if (count < r * c) return nums;                 //return original if output matrix is too big;
        return res;
    }

    public static void main(String[] args) {
        ReshapeTheMatrix tester = new ReshapeTheMatrix();
        int[][] input1 = {{375,18,-195,568,-767,-14,37,434,80,286,-805,654,88,-922,-189,500,782,651,-623},{301,463,357,487,555,821,-978,-630,649,-56,-618,407,405,-870,-629,-582,678,366,-453},{815,-927,-547,-990,-357,947,202,240,-476,130,710,-748,-192,154,-768,21,210,861,266},{-85,-126,400,-425,730,40,242,321,-774,-182,94,-230,-697,281,-526,80,-413,529,131},{-856,882,-168,287,513,-817,346,418,703,-985,-882,-70,-407,876,-779,495,-712,-979,-586},{-659,915,-37,618,795,-754,187,427,-654,-258,918,525,175,-265,-520,-993,446,926,846},{105,628,-494,211,747,670,-717,71,-860,476,-474,-168,566,8,106,-407,0,-524,-150},{-431,-652,495,553,-902,237,521,916,-542,-167,-242,676,667,674,-196,471,441,-453,743},{330,278,-899,237,-795,145,-41,591,-924,-526,-651,785,995,-358,-893,-664,-902,769,458}};
        int[][] input2 = {{1},
                        {2,3},
                        {4,5,6}};
        // MatrixPrinter.printMatrix(tester.matrixReshape(input1, 171, 1));
        Matrix.printMatrix(tester.matrixReshape(input2, 2, 3));
        Matrix.printMatrix(tester.matrixReshape(input2, 3, 4));

    }
}

这个整体的思路是很清晰的, 就是先压平成一维, 然后重新加到二维; 难点在于异常处理, 尤其是premature exit思路的使用;

为了写起来更简单, 直接先假设我们拿到的是一个 legal 的 input: 简单升级的思路; 然后再修改, 添加各种premature exit, 这样的思路写起来好很多;

当然这个时候一个很有诱惑的想法就是干脆一个2pass, 这个想法写起来就很简单了, 直接先把 nums 里面的个数全都数一遍,然后跟r*c比较就行了(注意这里有一个 subtlety, 如果 nums 里面的第二维长度不整齐, 按理说最后的程序最好也能处理).
但是这样一个2pass 的算法最后肯定不是最优解, 所以就不写了;

注意一个小问题, 在题目的描述里面说了, 要求 args 的范围都在[1,100], 这个不代表最后 judge 给你的 case 全都自动满足这个条件! 你自己的程序里面还是要检查一下, 也就是说这句话给你的不是一个条件, 而是一个任务, 是要你完成的部分;

这里二维坐标和一维坐标之间的转化计算一定要熟练,这个应该不用废话了;

整个代码应该还是很清晰的, 最后速度是8ms, 63%, 感觉一般化, 不过因为问题总体来说比较小,其实这个结果也差不多了;

看了一下别人提交的代码里面排名第一的那个代码, 他们的代码其实没有我这个好, 他们这个代码不支持 nums 所有的 column不整齐的情况;


Problem Description

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.

Example 1:
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.
Example 2:
Input:
nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]
Explanation:
There is no way to reshape a 2
2 matrix to a 2 * 4 matrix. So output the original matrix.
Note:
The height and width of the given matrix is in range [1, 100].
The given r and c are all positive.
Subscribe to see which companies asked this question.

Hide Tags Array

results matching ""

    No results matching ""