当前位置:网站首页>【LeetCode】Day94-重塑矩阵
【LeetCode】Day94-重塑矩阵
2022-07-05 06:13:00 【倒过来是圈圈】
题目
题解
二维数组的一维表示
题目需要我们做的事情相当于:
- 将二维数组 nums 映射成一个一维数组;
- 将这个一维数组映射回 r 行 c 列的二维数组。
具体原理是:对于 x∈[0,mn),第 x 个元素在 nums 中对应的下标为 (x / n,x % n),而在新的重塑矩阵中对应的下标为 (x / c,x % c)。所以我们在代码中做对应映射即可。
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int m=mat.length,n=mat[0].length;
if(m*n!=r*c)
return mat;
int[][] newMat=new int[r][c];
for(int num=0;num<m*n;num++){
newMat[num/c][num%c]=mat[num/n][num%n];
}
return newMat;
}
}
时间复杂度: O ( r c ) O(rc) O(rc)
空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
- PC register
- Leetcode-31: next spread
- Real time clock (RTC)
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- leetcode-22:括号生成
- A reason that is easy to be ignored when the printer is offline
- LVS简介【暂未完成(半成品)】
- LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
- Leetcode-6111: spiral matrix IV
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
猜你喜欢

Groupbykey() and reducebykey() and combinebykey() in spark

可变电阻器概述——结构、工作和不同应用

WordPress switches the page, and the domain name changes back to the IP address

Liunx starts redis

7. Processing the input of multidimensional features

LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树

Real time clock (RTC)

Overview of variable resistors - structure, operation and different applications

The connection and solution between the shortest Hamilton path and the traveling salesman problem

SQLMAP使用教程(一)
随机推荐
leetcode-556:下一个更大元素 III
1.14 - 流水线
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Flutter Web 硬件键盘监听
Liunx starts redis
Transform optimization problems into decision-making problems
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
1996. number of weak characters in the game
Leetcode-6111: spiral matrix IV
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Introduction et expérience de wazuh open source host Security Solution
1.13 - RISC/CISC
1041 Be Unique
leetcode-6109:知道秘密的人数
Leetcode-1200: minimum absolute difference
开源存储这么香,为何我们还要坚持自研?
一些工具的记录2022
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Sqlmap tutorial (II) practical skills I