LeetCode - 498. Diagonal Traverse
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.
class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
boolean up = true;
if (matrix.length == 0) return new int[0];
int[] res = new int[matrix.length * matrix[0].length];
int i = 0;
int j = 0;
for (int k = 0; k < matrix.length * matrix[0].length; k++) {
res[k] = matrix[i][j];
if (up) {
if ((i-1) >= 0 && (j+1) < matrix[0].length) {
i--;
j++;
} else if ((j+1) < matrix[0].length) {
j++;
up = false;
} else if ((i+1) < matrix.length) {
i++;
up = false;
} else break;
} else {
if ((i+1) < matrix.length && (j-1) >= 0) {
i++;
j--;
} else if ((i+1) < matrix.length) {
i++;
up = true;
} else if ((j+1) < matrix[0].length) {
j++;
up = true;
} else break;
}
}
return res;
}
}
LeetCode - 498. Diagonal Traverse的更多相关文章
- 【LeetCode】498. Diagonal Traverse 解题报告(Python)
[LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...
- 498. Diagonal Traverse对角线z型traverse
[抄题]: Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in dia ...
- 498 Diagonal Traverse 对角线遍历
详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...
- 498. Diagonal Traverse
题目思路 题目来源 C++实现 class Solution { public: vector<int> findDiagonalOrder(vector<vector<int ...
- Leetcode 498:对角线遍历Diagonal Traverse(python3、java)
对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. Given a matrix of M x N elemen ...
- [LeetCode] Diagonal Traverse 对角线遍历
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- python(leetcode)498. 对角线遍历
这题难度中等,记录下思路 第一个会超时, 第二个:思想是按斜对角线行进行右下左上交替遍历, def traverse(matrix): n=len(matrix)-1 m=len(matrix[0]) ...
- Java实现 LeetCode 498 对角线遍历
498. 对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 示例: 输入: [ [ 1, 2, 3 ], [ ...
随机推荐
- js中call和apply的作用和用法
call和apply的用途是完全一样的.改变函数中this的指向: 为什么要改变this的指向呢?这个有什么用?有哪些场景呢? 首先this的指向总是在变的,this的指向是由函数执行时所在的环境决定 ...
- python基础介绍二
一.python种类 1.1 Cpython python官方版本,使用c语言实现,运行机制:先编译,py(源码文件)->pyc(字节码文件),最终执行时先将字节码转换成机器码,然后交给cpu执 ...
- dubbo+zipkin调用链监控(二)
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- ArcGIS中国工具,版权声明,本人没有授权任何单位和个人销售,其他都是盗版,为了你个人和单位利益,请勿购买。 销售QQ:27652980,853740877,电话:18987281928,13108507190,qq群310964401
ArcGIS中国工具,版权声明,本人没有授权任何单位和个人销售,其他都是盗版,为了你个人和单位利益,请勿购买.销售QQ:27652980,853740877,电话:18987281928,131085 ...
- 基于ping++聚合支付进行微信红包开发
1.微信方面的开发,一定要详细的阅读微信支付的开发文档. https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13 ...
- python之使用set对列表去重,并保持列表原来顺序(转)
https://www.cnblogs.com/laowangball/p/8424432.html #原始方法,但是会打乱顺序 mylist = [1,2,2,2,2,3,3,3,4,4,4,4]m ...
- jQuery on()方法使用
jQuery on()方法 基本语法: 语法结构一: $(selector).on(event,function) 语法结构二: $(selector).on(events,[selector],[d ...
- spring mvc 实战化项目之三板斧
laravel实战化项目之三板斧 spring mvc 实战化项目之三板斧 asp.net mvc 实战化项目之三板斧 接上文希望从一张表(tb_role_info 用户角色表)的CRUD展开spri ...
- html5学习笔记2——新元素
一:H5的改变 <canvas> 新元素 标签 描述 <canvas> 标签定义图形,比如图表和其他图像.该标签基于 JavaScript 的绘图 API 新多媒体元素 标签 ...
- intellj idea 跑覆盖率