当前位置:网站首页>[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)
边栏推荐
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
- CPU内核和逻辑处理器的区别
- 1.13 - RISC/CISC
- WordPress switches the page, and the domain name changes back to the IP address
- 【Rust 笔记】15-字符串与文本(上)
- leetcode-31:下一个排列
- Introduction and experience of wazuh open source host security solution
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- [rust notes] 14 set (Part 1)
- 剑指 Offer II 058:日程表
猜你喜欢
Introduction et expérience de wazuh open source host Security Solution
[practical skills] how to do a good job in technical training?
leetcode-6111:螺旋矩阵 IV
Wazuh开源主机安全解决方案的简介与使用体验
1.14 - 流水线
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
SQLMAP使用教程(一)
Overview of variable resistors - structure, operation and different applications
Arduino 控制的 RGB LED 无限镜
MySQL advanced part 1: stored procedures and functions
随机推荐
Data visualization chart summary (I)
Currently clicked button and current mouse coordinates in QT judgment interface
Leetcode-1200: minimum absolute difference
Open source storage is so popular, why do we insist on self-development?
The sum of the unique elements of the daily question
leetcode-22:括号生成
【Rust 笔记】16-输入与输出(上)
LVS简介【暂未完成(半成品)】
One question per day 1765 The highest point in the map
MySQL advanced part 1: View
Sqlmap tutorial (II) practical skills I
SQLMAP使用教程(一)
Individual game 12
QT判断界面当前点击的按钮和当前鼠标坐标
JS quickly converts JSON data into URL parameters
1.14 - assembly line
Leetcode heap correlation
Shutter web hardware keyboard monitoring
Basic explanation of typescript
QQ computer version cancels escape character input expression