当前位置:网站首页>[leetcode] day94 reshape matrix
[leetcode] day94 reshape matrix
2022-07-05 06:14:00 【Upside down, it's a circle】
subject
566. Reshaping the matrix 【 Simple 】
Answer key
One dimensional representation of a two-dimensional array
What the topic requires us to do is equivalent to :
- Put two-dimensional array nums Map to a one-dimensional array ;
- Map this one-dimensional array back to r That's ok c Two dimensional array of columns .
The principle is : about x∈[0,mn), The first x The elements are nums The corresponding subscript in is (x / n,x % n), In the new remodeling matrix, the corresponding subscript is (x / c,x % c). So we can make corresponding mapping in the code .
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;
}
}
Time complexity : O ( r c ) O(rc) O(rc)
Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
猜你喜欢

Real time clock (RTC)

Appium automation test foundation - Summary of appium test environment construction

redis发布订阅命令行实现

Leetcode-6110: number of incremental paths in the grid graph

Leetcode array operation

Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135

SQLMAP使用教程(一)

Appium自动化测试基础 — Appium测试环境搭建总结
![R language [import and export of dataset]](/img/5e/a15ab692a6f049f846024c98820fbb.png)
R language [import and export of dataset]

Quickly use Amazon memorydb and build your own redis memory database
随机推荐
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
Redis publish subscribe command line implementation
wordpress切换页面,域名变回了IP地址
“磐云杯”中职网络安全技能大赛A模块新题
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Daily question 1688 Number of matches in the competition
【Rust 笔记】16-输入与输出(下)
WordPress switches the page, and the domain name changes back to the IP address
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
MIT-6874-Deep Learning in the Life Sciences Week 7
打印机脱机时一种容易被忽略的原因
1.13 - RISC/CISC
[rust notes] 16 input and output (Part 2)
js快速将json数据转换为url参数
4. 对象映射 - Mapping.Mapster
【Rust 笔记】13-迭代器(中)
MySQL advanced part 2: MySQL architecture
[rust notes] 15 string and text (Part 1)
927. 三等分 模拟
Golang uses context gracefully