[力扣] 算法 566 (C#)

566. 重塑矩阵

public class Solution {
    public int[][] MatrixReshape(int[][] nums, int r, int c) {
        if (nums.Length * nums[0].Length != r * c) return nums;
        var ret = new int[r][];
        for (var i = 0; i < r; i++)
            ret[i] = new int[c];
        for (int i = 0, m = 0, n = 0; i < nums.Length; i++)
            for (var j = 0; j < nums[0].Length; j++, n++, m += n / c, n %= c)
                ret[m][n] = nums[i][j];
        return ret;
    }
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注