当前位置:网站首页>[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)
边栏推荐
- Leetcode array operation
- 数据可视化图表总结(一)
- 【Rust 笔记】13-迭代器(下)
- Sqlmap tutorial (II) practical skills I
- LeetCode 1200. Minimum absolute difference
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
- leetcode-6111:螺旋矩阵 IV
- Leetcode stack related
- 2022年贵州省职业院校技能大赛中职组网络安全赛项规程
- Appium基础 — 使用Appium的第一个Demo
猜你喜欢
Redis publish subscribe command line implementation
Data visualization chart summary (II)
liunx启动redis
7. Processing the input of multidimensional features
1.13 - RISC/CISC
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
leetcode-6108:解密消息
MySQL advanced part 1: View
数据可视化图表总结(二)
[practical skills] technical management of managers with non-technical background
随机推荐
Basic explanation of typescript
【Rust 笔记】13-迭代器(中)
数据可视化图表总结(一)
LVS简介【暂未完成(半成品)】
【Rust 笔记】17-并发(上)
QQ computer version cancels escape character input expression
【Rust 笔记】16-输入与输出(上)
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
[rust notes] 14 set (Part 2)
Leetcode-9: palindromes
Individual game 12
Dichotomy, discretization, etc
leetcode-6111:螺旋矩阵 IV
4. 对象映射 - Mapping.Mapster
MySQL advanced part 1: View
Real time clock (RTC)
Groupbykey() and reducebykey() and combinebykey() in spark
Collection: programming related websites and books
LeetCode 0107. Sequence traversal of binary tree II - another method
[practical skills] how to do a good job in technical training?