当前位置:网站首页>Leetcode-498: diagonal traversal
Leetcode-498: diagonal traversal
2022-06-24 10:14:00 【Ugly and ugly】
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
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :mat = [[1,2],[3,4]]
Output :[1,2,3,4]
The problem solving process
Ideas and steps
(1)m * n Two dimensional matrix of , All in all m + n - 1 Diagonals , The traversal directions of adjacent diagonals are different ;
(2) Set the number of diagonal from top to bottom as [0,m + n − 2]. When i For even when , Traversal direction is from bottom left to top right ; When i In an odd number of , The traversal direction is from top right to bottom left ;
(3) When the first i Diagonals are traversed from bottom left to top right , namely i For even when , Each row index minus 1, Column index plus 1, Up to the edge of the matrix ;
When i < m when , At this time, the starting point of diagonal traversal is (i, 0);
When i ≥ m when , At this time, the starting point of diagonal traversal is (m − 1, i − m + 1);
(4) When the first i Diagonals are traversed from top right to bottom left , namely i In an odd number of , Every time the row index is added 1, Column index minus 1, Up to the edge of the matrix ;
When i < n when , At this time, the starting point of diagonal traversal is (0, i);
When i ≥ n when , Then the starting point of diagonal traversal is (i − n + 1, n − 1);
Code display
public class FindDiagonalOrder {
/** * Official answer : * m * n Two dimensional matrix of , All in all m + n - 1 Diagonals , The traversal directions of adjacent diagonals are different * Set the number of diagonal from top to bottom as [0,m + n − 2] * When i For even when , Traversal direction is from bottom left to top right ; * When i In an odd number of , The traversal direction is from top right to bottom left ; * * When the first i Diagonals are traversed from bottom left to top right , namely i For even when , Each row index minus 1, Column index plus 1, Up to the edge of the matrix ; * When i < m when , At this time, the starting point of diagonal traversal is (i, 0); * When i ≥ m when , At this time, the starting point of diagonal traversal is (m − 1, i − m + 1); * * When the first i Diagonals are traversed from top right to bottom left , namely i In an odd number of , Every time the row index is added 1, Column index minus 1, Up to the edge of the matrix ; * When i < n when , At this time, the starting point of diagonal traversal is (0, i); * When i ≥ n when , Then the starting point of diagonal traversal is (i − n + 1, n − 1); **/
public int[] findDiagonalOrder(int[][] mat) {
// That's ok
int m = mat.length;
// Column
int n = mat[0].length;
int[] result = new int[m * n];
int index = 0;
for (int i = 0; i < m + n - 1; i++) {
if (i % 2 == 0) {
// The even number of diagonals
int row = 0;
int line = 0;
if (i < m) {
row = i;
}
if (i >= m) {
row = m - 1;
line = i - m + 1;
}
while (row >= 0 && line < n) {
result[index] = mat[row][line];
row--;
line++;
index++;
}
} else {
// Odd diagonal
int row = 0;
int line = 0;
if (i < n) {
line = i;
}
if (i >= n) {
row = i - n + 1;
line = n - 1;
}
while (row < m && line >= 0) {
result[index] = mat[row][line];
row++;
line--;
index++;
}
}
}
return result;
}
public static void main(String[] args) {
int[][] mat = {
{
1,2,3},
{
4,5,6},
{
7,8,9}};
int[] result = new FindDiagonalOrder().findDiagonalOrder(mat);
for (int i = 0; i < result.length; i++) {
System.out.printf("%2d", result[i]);
}
System.out.println();
}
}
边栏推荐
- SQL sever基本数据类型详解
- Go language development environment setup +goland configuration under the latest Windows
- Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
- 为什么 JSX 语法这么香?
- How to standardize data center infrastructure management process
- port 22: Connection refused
- Nvisual digital infrastructure operation management software platform
- Cicflowmeter source code analysis and modification to meet requirements
- How to manage massive network infrastructure?
- Internet of things? Come and see Arduino on the cloud
猜你喜欢

简单的价格表样式代码

Getting user information for applet learning (getuserprofile and getUserInfo)

Internet of things? Come and see Arduino on the cloud

Cicflowmeter source code analysis and modification to meet requirements

How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?

CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance

正规方程、、、

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

5.菜品管理业务开发

Machine learning perceptron and k-nearest neighbor
随机推荐
2021-08-17
YOLOv6:又快又准的目标检测框架开源啦
学习使用php对字符串中的特殊符号进行过滤的方法
物联网?快来看 Arduino 上云啦
数组无缝滚动demo
Symbol.iterator 迭代器
分布式 | 如何与 DBLE 进行“秘密通话”
Machine learning - principal component analysis (PCA)
机器学习——主成分分析(PCA)
canvas管道动画js特效
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
canvas掉落的小球重力js特效动画
tf.errors
PHP encapsulates a file upload class (supports single file and multiple file uploads)
Nvisual digital infrastructure operation management software platform
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
Wechat cloud hosting launch public beta: in the appointment of the publicity meeting
el-table表格的拖拽 sortablejs
Regular matching mobile number
植物生长h5动画js特效
Input :nums = [2,7,11,15], target = 9