当前位置:网站首页>[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)
边栏推荐
- wordpress切换页面,域名变回了IP地址
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- Doing SQL performance optimization is really eye-catching
- Full Permutation Code (recursive writing)
- Appium foundation - use the first demo of appium
- RGB LED infinite mirror controlled by Arduino
- [rust notes] 15 string and text (Part 1)
- Liunx starts redis
- 1.13 - RISC/CISC
- Introduction et expérience de wazuh open source host Security Solution
猜你喜欢

leetcode-6108:解密消息

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition

1.14 - assembly line

Doing SQL performance optimization is really eye-catching

Full Permutation Code (recursive writing)

liunx启动redis

LaMDA 不可能觉醒吗?

MIT-6874-Deep Learning in the Life Sciences Week 7

Is it impossible for lamda to wake up?

Leetcode stack related
随机推荐
[rust notes] 16 input and output (Part 1)
Open source storage is so popular, why do we insist on self-development?
Leetcode array operation
【Rust 笔记】14-集合(上)
[practical skills] technical management of managers with non-technical background
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
LeetCode 0107. Sequence traversal of binary tree II - another method
MySQL advanced part 2: storage engine
leetcode-6109:知道秘密的人数
Appium foundation - use the first demo of appium
Daily question 2006 Number of pairs whose absolute value of difference is k
CPU内核和逻辑处理器的区别
Currently clicked button and current mouse coordinates in QT judgment interface
6. Logistic model
【Rust 笔记】17-并发(下)
Daily question 1688 Number of matches in the competition
MySQL advanced part 1: triggers
Introduction and experience of wazuh open source host security solution
[rust notes] 15 string and text (Part 1)
wordpress切换页面,域名变回了IP地址