This article is participating in the “Java Theme Month – Java Brush questions punch card”, see the activity link for details

Topic describes

In MATLAB, there is a very useful function 0 that remolds one matrix into another new matrix of a different size, but preserves its original data.

Give a matrix represented by a two-dimensional array, and two positive integers r and C, representing the number of rows and columns of the desired reconstructed matrix, respectively.

The reconstructed matrix requires that all elements of the original matrix be filled in the same row traversal order.

0 0 If the 0 0 operation with the given parameters is 0 0 0 0 0 Otherwise, output the original matrix.

Example 1: input: nums = [[1, 2], [3, 4]] r = 1, c = 4 output: [[1, 2, 3, 4]] : traveled through the nums as a result of [1, 2, 3, 4]. The new matrix is a 1 by 4 matrix, and the new matrix is filled row by row with the values of the previous elements. Source: LeetCode Link: https://leetcode-cn.com/problems/reshape-the-matrix Copyright belongs to The Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.Copy the code

Thought analysis

  • This problem is a matrix transformation problem, first can use naive algorithm to solve the problem. High time complexity!
  • The better solution to this problem is to use mathematical formulas to calculate the way, directly calculate the new position! High efficiency! Obviously, mathematics is very important in computer application!

AC code

public class DayCode {
    public static void main(String[] args) {
        int[][] nums = new int[] [] {{1.2}, {3.4}};
        int r = 1;
        int c = 4;
        int[][] ans = new DayCode().matrixReshape(nums, r, c);
        System.out.println("ans is " + Arrays.deepToString(ans));
    }
    
    /**
     * https://leetcode-cn.com/problems/reshape-the-matrix/
     * @param nums
     * @param r
     * @param c
     * @return* /
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int m = nums.length;
        int n = nums[0].length;
        if(m * n ! = r * c) {return nums;
        }

        int[][] ans = new int[r][c];
        int newX = 0;
        int newY = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                ans[newX][newY] = nums[i][j];
                newY++;
                if (newY >= c) {
                    newX++;
                    newY = 0; }}}return ans;
    }

    /**
     * https://leetcode-cn.com/problems/reshape-the-matrix/
     * @param nums
     * @param r
     * @param c
     * @return* /
    public int[][] matrixReshape2(int[][] nums, int r, int c) {
        int m = nums.length;
        int n = nums[0].length;
        if(m * n ! = r * c) {return nums;
        }

        int[][] ans = new int[r][c];
        for (int x = 0; x < m * n; x++) {
            ans[x / c][x % c] = nums[x / n][x % n];
        }
        returnans; }}Copy the code

Submit tests:

AC smoothly!

conclusion

  • Method one is O(n * n) in time, O(1) in space.
  • Method two is O(n) in time and O(1) in space.
  • When I wrote, I only thought of method one, not method two, keep going! Write and practice!
  • Stick to the daily question, come on!