当前位置:网站首页>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
};
边栏推荐
- TP-LINK enterprise router PPTP configuration
- 【嵌入式】使用JLINK RTT打印log
- LeetCode:394. 字符串解码
- Export IEEE document format using latex
- pytorch查看张量占用内存大小
- Crash problem of Chrome browser
- Excellent software testers have these abilities
- How to effectively conduct automated testing?
- LeetCode:剑指 Offer 04. 二维数组中的查找
- LeetCode:劍指 Offer 42. 連續子數組的最大和
猜你喜欢
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
优秀的软件测试人员,都具备这些能力
Nacos 的安装与服务的注册
深度剖析C语言指针
marathon-envs项目环境配置(强化学习模仿参考动作)
Sublime text using ctrl+b to run another program without closing other runs
[embedded] cortex m4f DSP Library
Cesium draw points, lines, and faces
Swagger setting field required is mandatory
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
随机推荐
gcc动态库fPIC和fpic编译选项差异介绍
How to conduct interface test? What are the precautions? Nanny level interpretation
Indentation of tabs and spaces when writing programs for sublime text
优秀的软件测试人员,都具备这些能力
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
LeetCode:26. 删除有序数组中的重复项
Navicat premium create MySQL create stored procedure
[NVIDIA development board] FAQ (updated from time to time)
随手记01
win10系统中的截图,win+prtSc保存位置
LeetCode:236. 二叉树的最近公共祖先
ESP8266-RTOS物联网开发
Excellent software testers have these abilities
企微服务商平台收费接口对接教程
[MySQL] limit implements paging
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Light of domestic games destroyed by cracking
Detailed explanation of heap sorting
使用latex导出IEEE文献格式
生成器参数传入参数