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.
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的更多相关文章

  1. 【LeetCode】498. Diagonal Traverse 解题报告(Python)

    [LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...

  2. 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 ...

  3. 498 Diagonal Traverse 对角线遍历

    详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...

  4. 498. Diagonal Traverse

    题目思路 题目来源 C++实现 class Solution { public: vector<int> findDiagonalOrder(vector<vector<int ...

  5. Leetcode 498:对角线遍历Diagonal Traverse(python3、java)

    对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. Given a matrix of M x N elemen ...

  6. [LeetCode] Diagonal Traverse 对角线遍历

    Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...

  7. [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse

    Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...

  8. python(leetcode)498. 对角线遍历

    这题难度中等,记录下思路 第一个会超时, 第二个:思想是按斜对角线行进行右下左上交替遍历, def traverse(matrix): n=len(matrix)-1 m=len(matrix[0]) ...

  9. Java实现 LeetCode 498 对角线遍历

    498. 对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 示例: 输入: [ [ 1, 2, 3 ], [ ...

随机推荐

  1. mvc中AntiForgeryToken的实现方式--看Mvc源码

    通过 AntiForgeryWorker的GetHtml()方法生成html --input hide元素--value=要验证的值,并生成cookie--用于保存需要验证的值. 类中的AntiFor ...

  2. unity-Profiler调试Android的正确姿势(mumu模拟器)

    1. 前置条件 安卓的相关环境 java.ant.sdk.ndk 什么的都装好(其实这里只需要 sdk 里面的 adb),配好 adb 工具的环境变量(意思就是 cmd 里直接输 adb 命令即可) ...

  3. shell字符串的用法

    shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ...

  4. 查看当前正在运行的python进程

    ps -ef |grep Python kill -9 pid  

  5. 前端 使用 crypto-js 对数据进行对称加密

    From:  https://www.cnblogs.com/CyLee/p/7216988.html 传送门: # crypto-js github https://github.com/brix/ ...

  6. Window下使用Charles对手机的Https请求进行抓包

    https://blog.csdn.net/zhaoerduo/article/details/52128607

  7. 【JavaScript从入门到精通】第三课 初探JavaScript魅力-03

    第三课 初探JavaScript魅力-03 函数传参 上节课的时候我们已经讲了什么是函数,实际上,函数在功能上就类似于css的class一样,将一段代码包裹起来使用.为了让函数的功能更加的丰富和实用, ...

  8. MongoDB下Map-Reduce使用简单翻译及示例

    目录 Map-Reduce JavaScript 函数 Map-Reduce 行为 一个简单的测试 原文地址https://docs.mongodb.com/manual/core/map-reduc ...

  9. SCF: 简单配置门面[转]

    原文:https://blog.csdn.net/koqizhao/article/details/82178100 Simple Configuration Facade :简单配置门面  是 代码 ...

  10. 在windows命令行批量ping局域网内IP

    参考了博客园Alfred Zhao的文章<Windows平台ping测试局域网所有在用IP> 在cmd命令行运行如下命令即可: ,,) -w .%i | find "回复&quo ...