498_Diagonal-Traverse
498_Diagonal-Traverse
Description
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
Explanation:

Note:
- The total number of elements of the given matrix will not exceed 10,000.
Solution
Java solution
class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
return new int[0];
}
int m = matrix.length, n = matrix[0].length;
int[] res = new int[m * n];
int row = 0, col = 0, d = 1;
for (int i = 0; i < m*n; i++) {
res[i] = matrix[row][col];
row -= d;
col += d;
// lower border
if (row >= m) {
row -= 1;
col += 2;
d = -d;
}
// right border
if (col >= n) {
col -= 1;
row += 2;
d = -d;
}
// upper border
if (row < 0) {
row = 0;
d = -d;
}
// left border
if (col < 0) {
col = 0;
d = -d;
}
}
return res;
}
}
Runtime: 7 ms. Your runtime beats 97.45 % of java submissions.
Python solution 1
class Solution:
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix == []:
return []
m, n = len(matrix), len(matrix[0])
coordinates = [(i, j) for i in range(m) for j in range(n)]
coordinates.sort(key=lambda x: sum(x) * max(m, n) - x[sum(x) % 2])
return [matrix[x][y] for x, y in coordinates]
Runtime: 164 ms. Your runtime beats 37.74 % of python3 submissions.
Python solution 2
class Solution:
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
entries = [(i + j, (j, i)[(i ^ j) & 1], val)
for i, row in enumerate(matrix)
for j, val in enumerate(row)]
return [e[2] for e in sorted(entries)]
Runtime: 136 ms. Your runtime beats 77.36 % of python3 submissions.
Python solution 3
class Solution:
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
m, n = len(matrix), len(matrix and matrix[0])
return [matrix[i][d-i]
for d in range(m+n-1)
for i in range(max(0, d-n+1), min(d+1, m))[::d%2*2-1]]
Runtime: 148 ms. Your runtime beats 59.43 % of python3 submissions.
498_Diagonal-Traverse的更多相关文章
- CSharp Algorithm - How to traverse binary tree by breadth (Part II)
/* Author: Jiangong SUN */ Here I will introduce the breadth first traversal of binary tree. The pri ...
- node to traverse cannot be null!
严重: Servlet.service() for servlet springmvc threw exception java.lang.IllegalArgumentException: node ...
- [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- 6.ZigZag Conversion(Graph, traverse)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 递归中traverse小人 & dc女王的区别
TRAVERSE 是一个小人, 拿着一个记事本, 顺着二叉树走, 走过一个, 在本子上面记下来 DIVIDE & CONQUER 是女王接到这个任务, 找两个小弟A和B, 让A和B先去收集, ...
- Traverse the dict in Python
We usually use the following 2 ways to traverse a dict: 1: for d in dic 2: for d in dic.keys() Which ...
- 814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...
- Traverse an expression tree and extract parameters
Traverse an expression tree and extract parameters I think as you've said that using ExpressionVis ...
- @babel/traverse 使用方法小记
@babel/traverse 官网: https://babeljs.io/docs/en/babel-traverse github:https://github.com/babel/babel/ ...
- 【LeetCode】498. Diagonal Traverse 解题报告(Python)
[LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...
随机推荐
- XEvent--Demo--使用XEvent来捕获在数据库DB1上发生的锁请求和锁释放
--==============================================================--使用XEvent来捕获在数据库DB1上发生的锁请求和锁释放--=== ...
- layui json数据格式要求
layui 数据格式要求 layui有自己的一套特定的数据格式交互(这很重要),必须参数code:0,msg:“”,count:数据size(int),data:”数据List”.一般我们选择封装返 ...
- 开发者常用的十款Chrome插件
本文是稀土掘金投稿,虽然其中有倔金的私货,是篇推广文,但我看过后认为内容确实不错,有些好插件还是第一次知道,对我很有帮助,考虑过后还是决定推荐给大家,最近我比较关注各种提高开发效率的工具与技巧,今后看 ...
- CF79D Password
题目链接 题意:给定长度为n的0/1序列,初始值都为0.你每次可以在给定的l个长度中的\(a_i\)并将序列中长度为\(a_i\)的部分取反.使得最终状态为\(x_1\)~\(x_k\),求最少取反次 ...
- objectARX创建 PaletteSet 停靠面板示例
objectARX创建 PaletteSet 停靠面板示例 图文By edata ,转载注明出处 http://www.cnblogs.com/edata 部分代码参考张帆<AutoCAD Ob ...
- memcache内存存储
memcache的内存分配默认是采用了Slab Allocator的机制分配.管理内存.在该机制出现以前,内存的分配是通过对所有记录简单地进行malloc和free来进行的. 但是,这种方式会导致内存 ...
- SpringMVC中重定向传参数的方法
在spring的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到.但是,这样使用的是forward方式,浏览器的地址栏是不变的,如 ...
- JS设计模式之单体模式(Singleton)
单体模式作为一种软件开发模式在众多面向对象语言中得到了广泛的使用,在javascript中,单体模式也是使用非常广泛的,但是由于javascript语言拥有其独特的面向对象方式,导致其和一些传统面向对 ...
- Apache环境修改.htaccess文件实现子目录强制HTTPS访问
如果要在Apache环境下实现子目录强制HTTPS地址访问,该怎么实现呢?在此文章中将与大家一起分享如何在Apache环境下修改.htaccess文件来实现子目录强制HTTPS地址访问. 1.根目录域 ...
- 【性能测试】:监控Mysql数据库方式
1, 进入到/etc目录下,打开my.cnf文件,在文件最后添加几行 slow_query_log = ON //打开慢查询开关 slow_q ...