当前位置:网站首页>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
};
边栏推荐
- C language double pointer -- classic question type
- 有效提高软件产品质量,就找第三方软件测评机构
- [Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
- PC easy to use essential software (used)
- [MySQL] limit implements paging
- Hutool gracefully parses URL links and obtains parameters
- Promise 在uniapp的简单使用
- Crash problem of Chrome browser
- After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
- 使用latex导出IEEE文献格式
猜你喜欢
Generator parameters incoming parameters
【嵌入式】Cortex M4F DSP库
Promise 在uniapp的简单使用
Current situation and trend of character animation
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
UML圖記憶技巧
Deep analysis of C language data storage in memory
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
The harm of game unpacking and the importance of resource encryption
深度剖析C语言指针
随机推荐
[MySQL] multi table query
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
Unsupported operation exception
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Deep analysis of C language pointer
torch建立的网络模型使用torchviz显示
[embedded] cortex m4f DSP Library
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
电脑清理,删除的系统文件
Cesium draw points, lines, and faces
LeetCode:387. 字符串中的第一个唯一字符
Simple use of promise in uniapp
TCP/IP协议
同一局域网的手机和电脑相互访问,IIS设置
LeetCode:498. 对角线遍历
Computer graduation design PHP Zhiduo online learning platform
After reading the programmer's story, I can't help covering my chest...
Double pointeur en langage C - - modèle classique
Light of domestic games destroyed by cracking
Trying to use is on a network resource that is unavailable