当前位置:网站首页>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
};
边栏推荐
- Deep analysis of C language pointer
- Esp8266-rtos IOT development
- LeetCode:214. 最短回文串
- C語言雙指針——經典題型
- Hutool gracefully parses URL links and obtains parameters
- egg. JS getting started navigation: installation, use and learning
- 目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
- LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
- 自动化测试框架有什么作用?上海专业第三方软件测试公司安利
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
猜你喜欢

Deep anatomy of C language -- C language keywords

Deep analysis of C language pointer

Indentation of tabs and spaces when writing programs for sublime text
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

MySQL uninstallation and installation methods

Problems in loading and saving pytorch trained models

LeetCode:221. 最大正方形

Using C language to complete a simple calculator (function pointer array and callback function)

【剑指offer】序列化二叉树

Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
随机推荐
Detailed explanation of heap sorting
sublime text中conda环境中plt.show无法弹出显示图片的问题
pytorch查看张量占用内存大小
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Crash problem of Chrome browser
Generator parameters incoming parameters
电脑F1-F12用途
【嵌入式】使用JLINK RTT打印log
Excellent software testers have these abilities
Revit secondary development Hof method calls transaction
LeetCode:26. 删除有序数组中的重复项
LeetCode:673. 最长递增子序列的个数
MySQL uninstallation and installation methods
【剑指offer】序列化二叉树
View computer devices in LAN
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
【ROS】usb_ Cam camera calibration
TP-LINK 企业路由器 PPTP 配置
LeetCode:劍指 Offer 42. 連續子數組的最大和
Warning in install. packages : package ‘RGtk2’ is not available for this version of R