当前位置:网站首页>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
};
边栏推荐
- poi追加写EXCEL文件
- 【嵌入式】Cortex M4F DSP库
- 游戏解包的危害及资源加密的重要性
- MySQL learning records 12jdbc operation transactions
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- China's high purity aluminum target market status and investment forecast report (2022 Edition)
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- TP-LINK enterprise router PPTP configuration
- ROS编译 调用第三方动态库(xxx.so)
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
猜你喜欢
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT

同一局域网的手机和电脑相互访问,IIS设置

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

PC easy to use essential software (used)

Warning in install. packages : package ‘RGtk2’ is not available for this version of R

Chrome浏览器的crash问题

JVM quick start

JVM performance tuning and practical basic theory - Part 1

The harm of game unpacking and the importance of resource encryption
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library
随机推荐
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Roguelike game into crack the hardest hit areas, how to break the bureau?
hutool优雅解析URL链接并获取参数
Generator parameters incoming parameters
Delay initialization and sealing classes
[brush questions] top101 must be brushed in the interview of niuke.com
同一局域网的手机和电脑相互访问,IIS设置
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
What is CSRF (Cross Site Request Forgery)?
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
电脑清理,删除的系统文件
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
Charging interface docking tutorial of enterprise and micro service provider platform
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Research and investment forecast report of citronellol industry in China (2022 Edition)
vb. Net changes with the window, scales the size of the control and maintains its relative position
JS native implementation shuttle box
Purpose of computer F1-F12
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
Cisp-pte practice explanation