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. sql--CONVERT、FOR XML PATH解决实际问题

    需求:每个平台分类下的门店,每个门店的名称.图片路径.评分,每个门店下的四个产品的名称.图片路径.评分 思路: 一开始门店动态化好写,用Ajax就行了.但是每个门店下面的产品,每个去请求一次查询有点不 ...

  2. 总结目前为止学到的关键字(break,continue,private,static,this,super,final,abstract)

    1.控制跳转语句:break(结束) 使用的场景: a.循环当中 b.switch break关键字需要注意的问题: 1.break关键字只能用于循环和switch语句当中,其本质就是结束整段语句的意 ...

  3. [uwp]MVVM模式实战之必应壁纸查看器

    最近学习MVVM,至于什么是MVVM我也在这儿不多说了,一是关于它的解释解释网上非常多,二是我怕自己讲不清,误导自己没关系,误导别人就不好了.. 好了,废话结束,看是实战...... 这个必应壁纸的d ...

  4. 201621123023《Java程序设计》第14周学习总结

    一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 由于我的系 ...

  5. luoguP5074 Eat the Trees

    https://www.luogu.org/problemnew/show/P5074 插头 $ dp $ 入门题 如果你还不会插头 $ dp $ 请右转 洛谷插头dp题解 虽然是入门题但还是逃不过分 ...

  6. 查看npm全局安装位置

    查看npm全局安装位置:npm config get prefix 设置位置:npm config set prefix 填写位置

  7. Unable to access 'default.path.data' (/var/lib/elasticsearch)

  8. canvas图像绘制过程中的注意

    特别来记录一下canvas绘制图像,要在图片加载完后,才会将其显示在canvas画布之上,否则会显示不出来:深刻体会,愣是找不到问题... var c=document.getElementById( ...

  9. canvas绘制简单的霓虹灯效果

    canvas简单动画分为三个步骤: 1.清除画布区域的内容: 2.重绘: 3.执行requestAnimationFrame(); 这个霓虹灯效果的demo,我没有用requestAnimationF ...

  10. SpringBoot整合MyBatis及Thymeleaf

    http://www.cnblogs.com/ludashi/archive/2017/05/08/6669133.html 上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建. ...