当前位置:网站首页>LeetCode 498. Diagonal traversal
LeetCode 498. Diagonal traversal
2022-06-26 09:43:00 【Stingy Wolf】
Title Description :
Give you a size of m x n Matrix mat , Please traverse diagonally , Use an array to return all the elements in the matrix .
Example :
Input :mat = [[1,2,3],[4,5,6],[7,8,9]]
Output :[1,2,4,7,5,3,6,8,9]
Ideas :
A matrix , Let's assume that there is n That's ok ,m Column , Then its diagonal ( refer to / The diagonal of this direction , instead of \) Altogether n + m - 1 strip . Suppose the diagonal number is from 0 Start , Then the number range of all diagonals is [0,n + m - 2], And it's easy to get a property , The number is i Point on the diagonal of , The sum of the horizontal and vertical coordinates is equal to i.
We can see from the sample data : Number i Is an even number of diagonals , The traversal direction is from bottom left to top right ; Number i Is the diagonal of an odd number , The traversal direction is from top right to bottom left .
From bottom left to top right , The change in the coordinates of a point is , The number of rows minus 1, The number of columns plus 1. namely x--,y++
From top right to bottom left , The change in the coordinates of a point is , Number of rows plus 1, Subtract the number of columns 1,. namely x++,y--
When a diagonal traversal is complete , We need to find the next point as a starting point , And flip the traversal direction .
Find the next point as the starting point , It can be discussed in different situations .
Set the number of the diagonals currently traversed as i
- When the direction is from bottom left to top right
- When
i < m - 1when , Next starting column , It must bei + 1, namelyy = i + 1, Go ahead , It can be directly based on the fact that the abscissa and ordinate of all points on the diagonal are constants , To figure it out , namelyx = (i + 1) - y - When
i >= m - 1when , Next starting column , Only to the last column , namelyy = m - 1, andx = (i + 1) - y
- When
- When the direction is from top right to bottom left
- When
i < n - 1when , Next starting line , It must bei + 1, namelyx = i + 1, andy = (i + 1) - x - When
i >= n - 1when , Next starting line , Only to the last line , namelyx = n - 1, andy = (i + 1) - x
- When
class Solution {
// Important nature : Points on the same diagonal , Its [x,y] The sum of the coordinates is fixed
// Number of diagonal lines , in total m + n - 1 strip
// The first i The sum of the coordinates on the diagonal lines is i
// Sum is even , Go to the upper right corner (x--, y++)
// Sum is odd , Go to the lower left corner (x++, y--)
public int[] findDiagonalOrder(int[][] mat) {
int n = mat.length, m = mat[0].length;
int[] ans = new int[n * m];
int k = 0;
int x = 0, y = 0;
for (int i = 0; i < n + m - 1; i++) {
if ((i & 1) == 0) {
// Take this line to the end
while (x >= 0 && y < m) ans[k++] = mat[x--][y++];
if (i < m - 1) y = i + 1;
else y = m - 1;
x = i + 1 - y;
} else {
while (x < n && y >= 0) ans[k++] = mat[x++][y--];
if (i < n - 1) x = i + 1;
else x = n - 1;
y = i + 1 - x;
}
}
return ans;
}
}
边栏推荐
- GAN Inversion: A Survey
- 【Sensors 2021】Relation-Based Deep Attention Network with Hybrid Memory for One-Shot Person Re-Id
- SQL高级教程
- How to correctly open the USB debugging and complete log functions of Huawei mobile phones?
- The 100000 line transaction lock has opened your eyes.
- Enter the page input box to automatically obtain the focus
- How to solve the problem that NVIDIA model cannot be viewed by inputting NVIDIA SMI and quickly view NVIDIA model information of computer graphics card
- Flink入门——单词统计
- Logview Pro can be used if the log is too large
- 同花顺炒股软件安全性怎样?在同花顺怎么开户
猜你喜欢

正则表达的学习

Creation and use of XSync synchronization script (taking debian10 cluster as an example)

Jz2440--- using uboot burning program

【CVPR 2021】DatasetGAN: Efficient Labeled Data Factory with Minimal Human Effort

MapReduce & yarn theory

"One week's work on digital power" -- encoder and decoder

How to create an IE tab in edge browser

"One week's study of model electricity" - capacitor, triode, FET

Construction practice of bank intelligent analysis and decision-making platform

Merrill Lynch data technology expert team | application of recommendation of relevant contents in group system data retrieval
随机推荐
Js--- get the data with the same key value in the object array to get a new array
js---获取对象数组中key值相同的数据,得到一个新的数组
LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改
thinkphp5手动报错
Comprehensive interpretation! Use of generics in golang
Install new version cmake & swig & tinyspline
online trajectory generation
英语常用短语
Collection object replication
thinkphp6.0的第三方扩展包,支持上传阿里云,七牛云
"One week's data collection" -- combinational logic circuit
Champions League data set (Messi doesn't cry - leaving Barcelona may reach another peak)
Common circuit design
教你用shell脚本检测服务器程序是否在运行
Kubernetes cluster deployment (v1.23.5)
Introduction to QPM
SQL query duplicate record
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
Enter the page input box to automatically obtain the focus
Spark based distributed parallel processing optimization strategy - Merrill Lynch data