【LeetCode】498. Diagonal Traverse 解题报告(Python)
【LeetCode】498. Diagonal Traverse 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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.
题目大意
沿着对角线遍历矩阵,返回结果。
解题方法
这个题做的时候想看别人怎么解答的,因为考察的就是细心嘛……但还是忍住了,坚持自己写完了。
做法很简单,有两个方向:向右上,向左下。
然后判断当出界了的时候,下一步应该向哪个方向走就行。因为矩阵的行列对称性,写出来的if语句肯定是对称的。这个可以方便自己写代码以及查错。
代码如下:
class Solution(object):
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix or not matrix[0]: return []
directions = [(-1, 1), (1, -1)]
count = 0
res = []
i, j = 0, 0
M, N = len(matrix), len(matrix[0])
while len(res) < M * N:
if 0 <= i < M and 0 <= j < N:
res.append(matrix[i][j])
direct = directions[count % 2]
i, j = i + direct[0], j + direct[1]
continue
elif i < 0 and 0 <= j < N:
i += 1
elif 0 <= i < M and j < 0:
j += 1
elif i < M and j >= N:
i += 2
j -= 1
elif i >= M and j < N:
j += 2
i -= 1
count += 1
return res
日期
2018 年 9 月 8 日 ———— 美好的周末,从刷题开始
【LeetCode】498. Diagonal Traverse 解题报告(Python)的更多相关文章
- LeetCode - 498. Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- kubectl logs查看日志时出现failed to create fsnotify watcher: too many open files
因为系统默认的 fs.inotify.max_user_instances=128 太小,在查看日志的pod所在节点重新设置此值: 临时设置 sudo sysctl fs.inotify.max_us ...
- python生成器浅析
A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...
- day15 内置函数和模块
day15 内置函数和模块 1.三元表达式 代码如下: x = 1 y = 2 res = 'ok' if x > y else 'no' print(res) 输出结果:no 2.内置函数:重 ...
- Linux学习 - ifconfig
ifconfig 1.功能 用来查看和配置网络设备,当网络环境发生改变时可通过此命令对网络进行相应的配置. 2.用法 ifconfig [网络设备] [参数] (1).参数 up 启动指定网络设备 ...
- 【JavaScript】创建全0的Array
1.创建一个长度为m的全0数组 var arr = new Array(m).fill(0); 2.创建一个m行n列的全0数组 var arr = new Array(m).fill(new Arra ...
- 【Matlab】abs不支持复整数
需要将uint8转换成double型数据才能计算 https://blog.csdn.net/lihe4151021/article/details/89372688 图像数据格式uint8与doub ...
- vue在某页面监听键盘输入事件
需求:在某一网页,通过上下左右键控制一些操作 实现: 1.基本代码: 因为没有绑定特定的元素.所以我们将事件绑定到document上. //当前页面监视键盘输入 document.onkeydown ...
- 安装Java1.8教程图解
在服务器上配置Java环境 第一步: 因为我们系统是centOS7 64位的,所以我下载的是最新版本的jdk X64位的 因为阿里云服务器事先就安装好了ssh,我们可以用xsell连上去,我把下载好的 ...
- 一文掌握 Python 的描述符协议
描述符介绍 描述符本质就是一个新式类,在这个新式类中,至少要实现了__get__(),__set__(),__delete__()中的一个.这也被称为描述符协议. class Myclass(obje ...
- shell 文件读取批量处理
cat ./redis-keys.txt | while read myline do echo ${myline} /usr/local/bin/redis-cli -h 000.00.00.000 ...