Leetcode-PHP题解D53-566-Reshape-the-Matrix

D53 566. Reshape the Matrix

题目链接

566. Reshape the Matrix

题目分析

给定一个二维数组,将它重新排列成rc列的二维数组。

思路

先把数据全部提出来,再用array_chunk函数重新分割数组。

最终代码

<?php
class Solution {

    /**
     * @param Integer[][] $nums
     * @param Integer $r
     * @param Integer $c
     * @return Integer[][]
     */
    function matrixReshape($nums, $r, $c) {
        $values = [];
        foreach($nums as $items){
            foreach($items as $item){
                $values[] = $item;
            }
        }
        return count($values)/$c==$r?array_chunk($values, $c):$nums;
    }
}

若觉得本文章对你有用,欢迎用爱发电资助。

评论

发表回复

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

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理