当前位置:网站首页>LeetCode:498. Diagonal traversal
LeetCode:498. Diagonal traversal
2022-07-06 08:50:00 【Bertil】
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 <= 10^4
- 1 <= m * n <= 10^4
- -10^5 <= mat[i][j] <= 10^5
Their thinking
1. First traverse the matrix , And then create a new one map To store key and value,key Is the sum of horizontal and vertical coordinates ,value by key An array of equal elements ( That is, an array of elements of each diagonal )
2. Then create a new result array res, Traverse map, Make the diagonal even ( That is to say 2 Diagonals 、 The first 4 Diagonals …, Satisfy key % 2 === 1) The elements of are placed in positive order res in , Make the diagonal an odd number ( That is to say 1 Diagonals 、 The first 3 Diagonals …, Satisfy key % 2 === 0) Put the elements of in reverse order res in , Finally back to res that will do
Code
/** * @param {number[][]} mat * @return {number[]} */
var findDiagonalOrder = function(mat) {
const row = mat.length
const col = mat[0].length
const record = new Map()
for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
const key = i + j
if (!record.has(key)) record.set(key, [])
record.get(key).push(mat[i][j])
}
}
const res = []
for (const [key, nums] of record.entries()) {
// entries() Get all key value pairs array :[[key1, value1], [key2, value2]]
key % 2 === 1 ? res.push(...nums) : res.push(...nums.reverse())
}
return res
};
边栏推荐
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 可变长参数
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Deep analysis of C language pointer
- 【嵌入式】Cortex M4F DSP库
- 企微服务商平台收费接口对接教程
- LeetCode:41. 缺失的第一个正数
- ESP8266-RTOS物联网开发
- POI add write excel file
猜你喜欢
随机推荐
The harm of game unpacking and the importance of resource encryption
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
What are the common processes of software stress testing? Professional software test reports issued by companies to share
visdom可视化实现与检查介绍
hutool优雅解析URL链接并获取参数
JS pure function
深度剖析C语言数据在内存中的存储
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Problems in loading and saving pytorch trained models
What is CSRF (Cross Site Request Forgery)?
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
Pytorch view tensor memory size
The mysqlbinlog command uses
Light of domestic games destroyed by cracking
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Restful API design specification
Export IEEE document format using latex
[MySQL] limit implements paging
Mongodb installation and basic operation
生成器参数传入参数








