当前位置:网站首页>[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 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
- Open source storage is so popular, why do we insist on self-development?
- The difference between CPU core and logical processor
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- 1.13 - RISC/CISC
- TypeScript 基础讲解
- Dichotomy, discretization, etc
- MySQL advanced part 2: MySQL architecture
- MySQL advanced part 1: stored procedures and functions
- 4. 对象映射 - Mapping.Mapster
猜你喜欢
Arduino 控制的 RGB LED 无限镜
开源存储这么香,为何我们还要坚持自研?
可变电阻器概述——结构、工作和不同应用
MIT-6874-Deep Learning in the Life Sciences Week 7
Individual game 12
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
Leetcode stack related
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Dynamic planning solution ideas and summary (30000 words)
liunx启动redis
随机推荐
Daily question 1189 Maximum number of "balloons"
liunx启动redis
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Dichotomy, discretization, etc
R language [import and export of dataset]
7. Processing the input of multidimensional features
leetcode-6111:螺旋矩阵 IV
Currently clicked button and current mouse coordinates in QT judgment interface
How to adjust bugs in general projects ----- take you through the whole process by hand
927. Trisection simulation
927. 三等分 模拟
Introduction to LVS [unfinished (semi-finished products)]
The sum of the unique elements of the daily question
1.14 - assembly line
New title of module a of "PanYun Cup" secondary vocational network security skills competition
[rust notes] 16 input and output (Part 1)
Leetcode-556: the next larger element III
MIT-6874-Deep Learning in the Life Sciences Week 7
LeetCode 1200.最小绝对差
LeetCode 0107. Sequence traversal of binary tree II - another method