当前位置:网站首页>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
};
边栏推荐
- visdom可视化实现与检查介绍
- 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
- Li Kou daily question 1 (2)
- MYSQL卸载方法与安装方法
- JS native implementation shuttle box
- LeetCode:39. 组合总和
- 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
- TP-LINK enterprise router PPTP configuration
- gcc动态库fPIC和fpic编译选项差异介绍
- 企微服务商平台收费接口对接教程
猜你喜欢
Trying to use is on a network resource that is unavailable
【ROS】usb_ Cam camera calibration
LeetCode:236. 二叉树的最近公共祖先
Deep anatomy of C language -- C language keywords
LeetCode:498. 对角线遍历
LeetCode:221. 最大正方形
电脑清理,删除的系统文件
Computer graduation design PHP Zhiduo online learning platform
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Unsupported operation exception
随机推荐
Compétences en mémoire des graphiques UML
【ROS】usb_ Cam camera calibration
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
TDengine 社区问题双周精选 | 第三期
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
有效提高软件产品质量,就找第三方软件测评机构
数学建模2004B题(输电问题)
gcc动态库fPIC和fpic编译选项差异介绍
Generator parameters incoming parameters
使用latex导出IEEE文献格式
Pytorch view tensor memory size
LeetCode:214. 最短回文串
Process of obtaining the electronic version of academic qualifications of xuexin.com
torch建立的网络模型使用torchviz显示
UML diagram memory skills
Tcp/ip protocol
Niuke winter vacation training 6 maze 2
hutool优雅解析URL链接并获取参数
Revit secondary development Hof method calls transaction