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:

  1. 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的更多相关文章

  1. 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 ...

  2. node to traverse cannot be null!

    严重: Servlet.service() for servlet springmvc threw exception java.lang.IllegalArgumentException: node ...

  3. [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse

    Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...

  4. 6.ZigZag Conversion(Graph, traverse)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. 递归中traverse小人 & dc女王的区别

    TRAVERSE 是一个小人, 拿着一个记事本, 顺着二叉树走, 走过一个, 在本子上面记下来 DIVIDE & CONQUER 是女王接到这个任务, 找两个小弟A和B, 让A和B先去收集, ...

  6. 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 ...

  7. 814. Binary Tree Pruning(leetcode) (tree traverse)

    https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...

  8. Traverse an expression tree and extract parameters

    Traverse an expression tree and extract parameters   I think as you've said that using ExpressionVis ...

  9. @babel/traverse 使用方法小记

    @babel/traverse 官网: https://babeljs.io/docs/en/babel-traverse github:https://github.com/babel/babel/ ...

  10. 【LeetCode】498. Diagonal Traverse 解题报告(Python)

    [LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...

随机推荐

  1. Mahout的taste里的几种相似度计算方法

    欧几里德相似度(Euclidean Distance) 最初用于计算欧几里德空间中两个点的距离,以两个用户x和y为例子,看成是n维空间的两个向量x和y,  xi表示用户x对itemi的喜好值,yi表示 ...

  2. Hadoop HDFS HA启动出现两个StandBy NameNode

    可能是zkfc服务没有启动,正确的流程如下: 1.在nn001上格式化zkfc sudo -u hdfs hdfs zkfc -formatZK 2.在三个(或以上)节点上启动journalnode ...

  3. linux学习之用户的切换

    普通用户: 输入su 用户名,点击Enter Root用户: 输入su root,点击Enter 输入登录密码,点击Enter

  4. Android 多线程基础

    需要注意几个概念:Runnable,Thread,Handler. 1. Runnable只是一个接口,里面包含run()函数.所以Runnable本身不会开启线程. 2. Thread实现Runna ...

  5. hadoop-1.2.1-1.x86_64.rpm、jdk-7u45-linux-x64.tar.gz安装(64位)

    一,   配置信息 机器是64位,所以操作系统.软件都是64位的. 操作系统:CentOS6.2(64位): Hadoop是hadoop-1.2.1-1.x86_64.rpm: JDK是jdk-7u4 ...

  6. Service由浅到深——AIDL的使用方式

    前言 最近有很多朋友问我这个AIDL怎么用,也许由于是工作性质的原因,很多人都没有使用过aidl,所以和他们讲解完以后,感觉对方也是半懂不懂的,所以今天我就从浅到深的分析一下这个aidl具体是怎么用的 ...

  7. [CSS3] 边栏导航动画

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. jdk(1.8)命令行工具(二)

    2.3 jinfo:java配置信息工具 jinfo(Configuration Info for Java)的作用是实时的查看和调整虚拟机的各项参数.使用jps -v 可以查看虚拟机启动时显示指定的 ...

  9. Linux(ubuntu18.04)切换python版本

    前言 Ubuntu18.04系统在安装python时会安装两个版本:2.7和3.6.默认情况下系统环境使用的是python2,但是我们有时需要使用python3来作为我们的开发环境,所以需要自由切换p ...

  10. O01-Linux CentOS7中利用RDO部署OpenStack

    一.前言 1.RDO是红帽Red Hat 的一个开源项目,全称是RPM Distribution of OpenStack,能够帮助我们快捷部署OpenStack项目. 官方部署文档:https:// ...