当前位置:网站首页>LeetCode:498. 对角线遍历
LeetCode:498. 对角线遍历
2022-07-06 08:44:00 【Bertil】
给你一个大小为 m x n 的矩阵 mat ,请以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
示例 1:
输入:mat = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,4,7,5,3,6,8,9]
示例 2:
输入:mat = [[1,2],[3,4]]
输出:[1,2,3,4]
提示:
- m == mat.length
- n == mat[i].length
- 1 <= m, n <= 10^4
- 1 <= m * n <= 10^4
- -10^5 <= mat[i][j] <= 10^5
解题思路
1.首先遍历矩阵,然后新建一个map来存储key和value,key为横纵坐标之和,value为key相等的元素数组(即每条对角线的元素组成的数组)
2.然后新建一个结果数组res,遍历map,将对角线是偶数(即第2条对角线、第4条对角线…,满足key % 2 === 1)的元素正序放入res中,将对角线是奇数(即第1条对角线、第3条对角线…,满足key % 2 === 0)的元素倒序放入res中,最后返回res即可
代码
/** * @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()获取所有的键值对数组:[[key1, value1], [key2, value2]]
key % 2 === 1 ? res.push(...nums) : res.push(...nums.reverse())
}
return res
};
边栏推荐
- Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
- Unsupported operation exception
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- JS inheritance method
- JS native implementation shuttle box
- Charging interface docking tutorial of enterprise and micro service provider platform
- [brush questions] top101 must be brushed in the interview of niuke.com
- JVM performance tuning and practical basic theory - Part 1
- hutool优雅解析URL链接并获取参数
- Function coritization
猜你喜欢

Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures

Sublime text using ctrl+b to run another program without closing other runs

Marathon envs project environment configuration (strengthen learning and imitate reference actions)

marathon-envs项目环境配置(强化学习模仿参考动作)
![[MySQL] log](/img/e9/1617122888c096cf6aba2bdb88f0ef.png)
[MySQL] log

FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战

Chrome浏览器的crash问题

pytorch训练好的模型在加载和保存过程中的问题

Deep analysis of C language data storage in memory

个人电脑好用必备软件(使用过)
随机推荐
查看局域网中电脑设备
JVM performance tuning and practical basic theory - Part 1
Visual implementation and inspection of visdom
LeetCode:剑指 Offer 03. 数组中重复的数字
China polyether amine Market Forecast and investment strategy report (2022 Edition)
Promise 在uniapp的简单使用
Problems in loading and saving pytorch trained models
Unsupported operation exception
The network model established by torch is displayed by torch viz
Double pointeur en langage C - - modèle classique
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
The harm of game unpacking and the importance of resource encryption
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
Computer cleaning, deleted system files
Colorlog combined with logging to print colored logs
【嵌入式】使用JLINK RTT打印log
Crash problem of Chrome browser
Roguelike游戏成破解重灾区,如何破局?
Detailed explanation of heap sorting
UnsupportedOperationException异常