D53 566. Reshape the Matrix
题目链接
566. Reshape the Matrix
题目分析
给定一个二维数组,将它重新排列成r
行c
列的二维数组。
思路
先把数据全部提出来,再用array_chunk
函数重新分割数组。
最终代码
<?phpclass 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; }}
若觉得本文章对你有用,欢迎用爱发电资助。