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膏集05
JS膏集05 1.复习 闭包内的函数也可以使用参数 闭包的建议写法 ) 2.浅拷贝 相当于把一个对象中的所有的内容复制一份给另一个对象.直接复制. 或者说,就是把一个对象的地址给了另一个对象,它们指向 ...
- What is the NETStandard.Library metapackage?
In my last post, I took a quick look at the Microsoft.AspNetCore meta package. One of the libraries ...
- .Net转Java.08.format
%[index$][标识][最小宽度]转换方式 [index$]可以用于表示对第index个参数进行格式化, // Java代码 String s1=String.format("%3$s, ...
- .Net转Java.07.IDEA和VS常用操作、快捷键对照表
功能 IDEA 2017.1 快捷键 Visual Studio 2015 快捷键 文档 格式化整个文档 Ctrl+Alt+L Ctrl+E,D 或者 Ctrl+K,D 文件 显示最近的 ...
- [PHP] 多表外连接性能测试及优化
原文:https://blog.csdn.net/tang_huan_11/article/details/41925639 版权声明:本文为博主原创文章,转载请附上博文链接!
- ceph:如何处理rados --striper上传失败的对象
如何处理使用rados --striper上传失败的对象? `Remove striped objects from a ceph pool without using the striper lib ...
- 你真的会打 Log 吗
前言 工程师在日常开发工作中,更多的编码都是基于现有系统来进行版本迭代.在软件生命周期中,工程维护的比重也往往过半.当我们维护的系统出现问题时,第一时间想到的是查看日志来判断问题原因,这时候日志记录如 ...
- Spring 拦截器postHandle无法修改Response的原因
如果controller跳转至页面,postHandle是没问题的. 如果@ResponseBody注释 或者返回 ResponseEntity,在postHandle拦截器中修改请求头,是无效的. ...
- Substr与mb_substr区别
<?php $str = substr('helloword',3,4);//从下标3开始截取截取4个字符 $str = substr('helloword',3);//从截取掉前三个字符 ...
- 自建k8s集群日志采集到阿里云日志服务
自建k8s集群 的master 节点安装 logtail 采集工具 wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.c ...