[力扣] 算法 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;
    }
}

发表回复

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

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据