We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接: https://leetcode-cn.com/problems/diagonal-traverse
难度: Medium 标签: 数组 矩阵 模拟
Medium
数组
矩阵
模拟
The text was updated successfully, but these errors were encountered:
Difficulty: 中等
Related Topics: 数组, 矩阵, 模拟
给你一个大小为 m x n 的矩阵 mat ,请以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
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
思路:
i + j
Language: JavaScript
/** * @param {number[][]} mat * @return {number[]} */ var findDiagonalOrder = function(mat) { const [m, n] = [mat.length, mat[0].length]; const nums = []; for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { (i + j) in nums ? nums[i + j].push(mat[i][j]) : nums[i + j] = [mat[i][j]]; } } return nums.reduce((arr, curr, i) => { !(i % 2) ? arr.push(...curr.reverse()) : arr.push(...curr); return arr; }, []) };
Sorry, something went wrong.
No branches or pull requests
题目链接: https://leetcode-cn.com/problems/diagonal-traverse
难度:
Medium
标签:
数组
矩阵
模拟
The text was updated successfully, but these errors were encountered: