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. Django REST framework API开发

    RESTful设计方法 1. 域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https:/ ...

  2. angular $digest already in progress

    angular.js:11706 Error: [$rootScope:inprog] $digest already in progresshttp://errors.angularjs.org/1 ...

  3. CSS魔法堂:更丰富的前端动效by CSS Animation

    前言  在<CSS魔法堂:Transition就这么好玩>中我们了解到对于简单的补间动画,我们可以通过transition实现.那到底多简单的动画适合用transtion来实现呢?答案就是 ...

  4. js 图片base64转file文件的两种方式

    js 图片base64转file文件的两种方式 https://blog.csdn.net/yin13037173186/article/details/83302628 //将base64转换为bl ...

  5. SSE图像算法优化系列九:灵活运用SIMD指令16倍提升Sobel边缘检测的速度(4000*3000的24位图像时间由480ms降低到30ms)。

    这半年多时间,基本都在折腾一些基本的优化,有很多都是十几年前的技术了,从随大流的角度来考虑,研究这些东西在很多人看来是浪费时间了,即不能赚钱,也对工作能力提升无啥帮助.可我觉得人类所谓的幸福,可以分为 ...

  6. python接口自动化测试(二)-requests.get()

    环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括: requests常用请求方法使用,包括:get,post requests库中的Session.Cookie的使用 其它 ...

  7. Android BLE蓝牙详细解读

    代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...

  8. 阿里云 nginx配置ssl证书实现https访问

    一,环境说明 服务器系统:ubuntu16.04LTS 服务器IP地址:47.89.12.99 域名:bjubi.com 二,域名解析到服务器 在阿里云控制台-产品与服务-云解析DNS-找到需要解析的 ...

  9. numpy累积

    numpy累积有两类函数:np.cumxxxxx和np.ufunc.accumulate() import numpy as np a = np.arange(1, 5) print(np.cumpr ...

  10. Vue2的独立构建与运行时构建的差别

    其实这个问题在你使用vue-cli构建项目的时候是不会出现的,因为你在创建项目的构建过程中已经让你勾选了,然后会写入webpack.config.js中. 这就在这,会让你选择Vue的构建方式. 如果 ...