美文网首页
Leetcode #566. Reshape the Matri

Leetcode #566. Reshape the Matri

作者: 尴尴尬尬先生 | 来源:发表于2017-05-04 14:43 被阅读0次
public static int[][] matrixReshape(int[][] nums, int r, int c) {
        int len_1 = nums.length,len_2 = nums[0].length;
        int[][] result = new int[r][c];
        if(len_1*len_2!=r*c)
            return nums;
        for(int i=0;i<r*c;i++){
            //利用reshape的矩阵的下标和原下标的关系
            result[i/c][i%c]=nums[i/len_2][i%len_2];
        }
        return result;
        
    }

直接利用新矩阵和原矩阵的下标关系,直接赋值。

相关文章

网友评论

      本文标题:Leetcode #566. Reshape the Matri

      本文链接:https://www.haomeiwen.com/subject/zxzrtxtx.html