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 ...
随机推荐
- linux的定制和发布(一)
如果总是仰视高山,就会挫伤我们攀登的勇气,使我们固步自封.我们需要做的就 是迈开自己的脚步,踏出第一步,let's go! Linux的裁剪一般有三种方法: 1.以一个已经安装好的系统为基 ...
- 三.jenkins 在windows上配置master 和 agent(slave)
参考链接: https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+mach ...
- c# 线程的基本使用
创建线程 线程的基本操作 线程和其它常见的类一样,有着很多属性和方法,参考下表: 创建线程的方法有很多种,这里我们先从thread开始创建线程 class Program { static void ...
- iOS 模拟器网络设置
在 iOS Simulator 下,点上面菜单栏最左面的苹果标志,第三项 网络设置,然后先最后面一项,网络设置. 然后在网络设置界面下可以开启或关闭网络.
- java—过虑器基础(47)
在web项目中就只有三大组件: Filter过虑器 监听器. Servlet 在web中过虑器就是一个类javax.servlet.Filter. 过虑器是用于在执行时,过虑用户的请求(request ...
- java—将查询的结果封装成List<Map>与用回调函数实现数据的动态封装(44)
手工的开始QueryRunner类.实现数据封装: MapListHandler MapHandler BeanListHandler BeanHandler 第一步:基本的封装测试 写一个类,Que ...
- pyppeteer初尝滋味
最近在爬几个电商平台网站用的selenium一登录就会有验证,目前这些网站对selenium检测很严格 因为不少大网站有对selenium的js监测机制.比如navigator.webdriver,n ...
- Echart自定义属性3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 洛谷P5292 [HNOI2019]校园旅行(二分图+最短路)
题面 传送门 题解 如果暴力的话,我们可以把所有的二元组全都扔进一个队列里,然后每次往两边更新同色点,这样的话复杂度是\(O(m^2)\) 怎么优化呢? 对于一个同色联通块,如果它是一个二分图,我们只 ...
- 用flask实现一个用户登录的功能
#!/usr/bin/python #coding=utf-8 from flask import Flask,session,redirect,url_for,request app=Flask(_ ...