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;
    }
}

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


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Leetcode PHP题解--D52 496. Next Greater Element I

>> 下一篇: Leetcode基础刷题之(94. Binary Tree Inorder Traversal)