当前位置:网站首页>【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)
边栏推荐
- Open source storage is so popular, why do we insist on self-development?
- Appium foundation - use the first demo of appium
- Basic explanation of typescript
- 开源存储这么香,为何我们还要坚持自研?
- 【Rust 笔记】14-集合(上)
- leetcode-22:括号生成
- Daily question 2013 Detect square
- Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
- Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
- Leetcode-6109: number of people who know secrets
猜你喜欢

1.13 - RISC/CISC

Wazuh開源主機安全解决方案的簡介與使用體驗

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Dichotomy, discretization, etc

API related to TCP connection

Appium自动化测试基础 — Appium测试环境搭建总结

数据可视化图表总结(二)

Appium基础 — 使用Appium的第一个Demo

7. Processing the input of multidimensional features

Open source storage is so popular, why do we insist on self-development?
随机推荐
927. 三等分 模拟
[cloud native] record of feign custom configuration of microservices
R language [import and export of dataset]
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Convolution neural network -- convolution layer
Daily question 2013 Detect square
Appium foundation - use the first demo of appium
The sum of the unique elements of the daily question
开源存储这么香,为何我们还要坚持自研?
MIT-6874-Deep Learning in the Life Sciences Week 7
WordPress switches the page, and the domain name changes back to the IP address
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
Leetcode-3: Longest substring without repeated characters
Sqlmap tutorial (1)
wordpress切换页面,域名变回了IP地址
PC register
Appium基础 — 使用Appium的第一个Demo
Arduino 控制的 RGB LED 无限镜
【Rust 笔记】15-字符串与文本(上)
[rust notes] 16 input and output (Part 2)