当前位置:网站首页>Force deduction solution summary 498 diagonal traversal
Force deduction solution summary 498 diagonal traversal
2022-07-22 19:31:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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 1:
Input :mat = [[1,2,3],[4,5,6],[7,8,9]]
Output :[1,2,4,7,5,3,6,8,9]
Example 2:
Input :mat = [[1,2],[3,4]]
Output :[1,2,3,4]
Tips :
m == mat.length
n == mat[i].length
1 <= m, n <= 104
1 <= m * n <= 104
-105 <= mat[i][j] <= 105
source : Power button (LeetCode)
link :https://leetcode.cn/problems/diagonal-traverse
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Set both directions , They are upper left and lower right .forward==0 Hour represents upper left ,forward==1 Hour stands for bottom right . * Take the upper left search as an example , If (x+1,y-1) Not out of range , Then add this value to the array , otherwise , This indicates that it is time to reverse the search . * hold forward Set to 1.
Code :
public class Solution498 {
public int[] findDiagonalOrder(int[][] mat) {
int index = 0;
int forward = 0;//0 The upper right ,1 The lower left
int[] result = new int[mat[0].length * mat.length];
int x = 0, y = 0;
while (index < result.length) {
System.out.println("y:" + y + ",x:" + x);
result[index++] = mat[y][x];
if (forward == 0) {
if ((x + 1) < mat[0].length && (y - 1) >= 0) {
x++;
y--;
continue;
}
if ((x + 1) < mat[0].length) {
x++;
} else {
y++;
}
forward = 1;
continue;
}
if ((x - 1) >= 0 && (y + 1) < mat.length) {
x--;
y++;
continue;
}
if (y + 1 < mat.length) {
y++;
} else {
x++;
}
forward = 0;
}
return result;
}
}
边栏推荐
- Several trends in the future development of software industry
- 关于人力外包公司那些事
- 十年架构五年生活-05第一次出差
- Execute function now
- Leetcode daily question 2021/12/13-2021/12/19
- Leetcode: 596. Classes with more than 5 students
- Flutter 通过拖拽调整绘制图形
- Global scope and function scope
- “35岁,我退休了”:关于中年危机,这是最靠谱的回答
- Leetcode daily question 2022/3/28-2022/4/3
猜你喜欢
Mysql5.7 decompression configuration steps
[yolov5 practice 4] traffic sign recognition system based on yolov5 - model test and evaluation
Grafana panel - about conversion
Numpy.reshape finish image cutting
Industrial router oilfield wireless monitoring
融云漫话:没有一个人躲得过“视频会议”
MFC对话框程序只运行单个实例 的简单示例
音频 3A 处理实践,让你的应用更「动听」
Nacos persistent connection MySQL database SM4 encryption scheme
Scenario practice | how to use rongyun super group to build a game community
随机推荐
arguments
Swagger-UI介绍及常用注解说明
Leetcode: 197. rising temperature
Create objects using factory methods
Leetcode daily question 2022/1/10-2022/1/16
Introduction to arrays
河北、浙江的气候变化了?
LeetCode 每日一题 2022/2/7-2022/2/13
Flutter's first program Hello world!
JS advanced - understanding of functions
Industrial router oilfield wireless monitoring
关于红队方面的学习资料
Numpy finding the mean value of non-zero elements of matrix
Unity: quick positioning camera
Programmer interview golden code interview question 01.03. URL
LeetCode 每日一题 2022/3/7-2022/3/13
An analysis of the CPU surge of an RFID tag management system in.Net
Programmer interview golden code interview question 01.04. palindrome arrangement
Swagger UI introduction and common notes
记一次 .NET 某RFID标签管理系统 CPU 暴涨分析