当前位置:网站首页>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
};
边栏推荐
- Computer cleaning, deleted system files
- Browser thread
- 电脑清理,删除的系统文件
- LeetCode:26. 删除有序数组中的重复项
- PC easy to use essential software (used)
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
- 优秀的软件测试人员,都具备这些能力
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- Roguelike游戏成破解重灾区,如何破局?
- 有效提高软件产品质量,就找第三方软件测评机构
猜你喜欢
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Indentation of tabs and spaces when writing programs for sublime text
JS inheritance method
Sublime text using ctrl+b to run another program without closing other runs
What is CSRF (Cross Site Request Forgery)?
egg. JS getting started navigation: installation, use and learning
Light of domestic games destroyed by cracking
同一局域网的手机和电脑相互访问,IIS设置
Roguelike游戏成破解重灾区,如何破局?
Screenshot in win10 system, win+prtsc save location
随机推荐
@JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
[MySQL] log
marathon-envs项目环境配置(强化学习模仿参考动作)
Swagger setting field required is mandatory
Esp8266-rtos IOT development
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
Navicat premium create MySQL create stored procedure
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
What are the common processes of software stress testing? Professional software test reports issued by companies to share
Revit secondary development Hof method calls transaction
【Nvidia开发板】常见问题集 (不定时更新)
移位运算符
Sublime text using ctrl+b to run another program without closing other runs
可变长参数
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)