498 Diagonal Traverse 对角线遍历
详见:https://leetcode.com/problems/diagonal-traverse/description/
C++:
class Solution {
public:
vector<int> findDiagonalOrder(vector<vector<int>>& matrix)
{
if (matrix.empty() || matrix[0].empty())
{
return {};
}
int m = matrix.size(), n = matrix[0].size(), r = 0, c = 0, k = 0;
vector<int> res(m * n);
vector<vector<int>> dirs{{-1,1}, {1,-1}};
for (int i = 0; i < m * n; ++i)
{
res[i] = matrix[r][c];
r += dirs[k][0];
c += dirs[k][1];
if (r >= m)
{
r = m - 1;
c += 2;
k = 1 - k;
}
if (c >= n)
{
c = n - 1;
r += 2;
k = 1 - k;
}
if (r < 0)
{
r = 0;
k = 1 - k;
}
if (c < 0)
{
c = 0;
k = 1 - k;
}
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/6414461.html
498 Diagonal Traverse 对角线遍历的更多相关文章
- [LeetCode] Diagonal Traverse 对角线遍历
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- 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 ...
- 【LeetCode】498. Diagonal Traverse 解题报告(Python)
[LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...
- LeetCode - 498. Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- 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 ...
- [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- LeetCode:对角线遍历【498】
LeetCode:对角线遍历[498] 题目描述 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 示例: 输入: [ [ ...
- 498. (leetcode)对角线遍历
498. 对角线遍历 根据题目的图像看,主要有两种走法,第一种是向右上(顺时针方向),第二种是向左下(逆时针)走 我们设 x ,y初始为0,分别对应横纵坐标 现在分析右上(0,2) 为例:(注意右上的 ...
随机推荐
- 在Windows上使用libcurl发起HTTP请求
curl下载地址https://curl.haxx.se/download.html 当前最新版本为7.61.0 将下载的curl-7.61.0.zip解压,得到curl-7.61.0 在目录curl ...
- 使用C语言获取字符串或文件的MD5值
libmd5地址:https://sourceforge.net/projects/libmd5-rfc/ MD5Demo1.c #include <stdio.h> #include & ...
- 开源企业IM免费企业即时通讯ENTBOOST V2014.177版本号正式公布
版权声明:本文为博主原创文章,欢迎转载,转载请尽量保持原文章完整,谢谢! https://blog.csdn.net/yanghz/article/details/30529469 ENTBOOST, ...
- oracle性能监控
https://blog.csdn.net/yangshangwei/article/details/52449489#监控事例的等待 https://blog.csdn.net/yangshangw ...
- Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input ...
- access函数的使用检查文件的权限【学习笔记】
#include "apue.h" #include <fcntl.h> int main(int argc,char **argv) { ) err_quit(&qu ...
- POJ1228:Grandpa's Estate(给定一些点,问是否可以确定一个凸包)
Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandp ...
- robotframework:appium切换webview后,在第一个页面操作成功,跳转到第二个页面后,执行命令失败
问题: 在用robot写手机淘宝app的自动化时,打开手机淘宝后,点击天猫国际,跳转到天猫国际页面,天猫国际页面是H5, 需要切换到对应的webview,切换到webview后,点击美妆菜单,跳转到美 ...
- 转载:SharePoint:扩展DVWP - 第1部分:布局增强 – 在默认值模板和编辑模板中重新排列栏
SharePoint:扩展DVWP - 第1部分:布局增强 – 在默认值模板和编辑模板中重新排列栏 当我们在数据视图中启用编辑,删除模式的链接时,SPD总是将链接添加到左边. 而我本来希望添加到右侧. ...
- Collection View Programming Guide for iOS---(二)----Collection View Basics
Collection View Basics Collection View 基础 To present its content onscreen, a collection view coope ...