当前位置:网站首页>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的编写程序时的Tab和空格缩进问题
- 移位运算符
- TDengine 社区问题双周精选 | 第三期
- gcc动态库fPIC和fpic编译选项差异介绍
- What is CSRF (Cross Site Request Forgery)?
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- How to conduct interface test? What are the precautions? Nanny level interpretation
- vb. Net changes with the window, scales the size of the control and maintains its relative position
- MySQL learning record 10getting started with JDBC
- LeetCode:236. 二叉树的最近公共祖先
猜你喜欢

【嵌入式】Cortex M4F DSP库

C语言深度解剖——C语言关键字

Precise query of tree tree

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

MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object

JVM performance tuning and practical basic theory - Part 1

JVM quick start

TCP/IP协议

Promise 在uniapp的简单使用

Delay initialization and sealing classes
随机推荐
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
Detailed explanation of heap sorting
Mobile phones and computers on the same LAN access each other, IIS settings
C language double pointer -- classic question type
China polyether amine Market Forecast and investment strategy report (2022 Edition)
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
Is it safe to open an account in Zheshang futures?
[NVIDIA development board] FAQ (updated from time to time)
TCP/IP协议
Promise 在uniapp的简单使用
sublime text中conda环境中plt.show无法弹出显示图片的问题
LeetCode:673. 最长递增子序列的个数
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
China vanadium battery Market Research and future prospects report (2022 Edition)
游戏解包的危害及资源加密的重要性
Restful API design specification
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
LeetCode:剑指 Offer 04. 二维数组中的查找
生成器参数传入参数
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines