498. Diagonal Traverse
题目思路
题目来源C++实现
class Solution
{
public:
vector<int> findDiagonalOrder(vector<vector<int>>& matrix)
{
if(matrix.empty())
{
return {};
}
vector<int> result;
bool fromUp = false; /* false 自下而上 ture 自上而下*/
int a = 0;
int b = 0; //(a,b) A点
int c = 0;
int d = 0; //(c,d) B点
int endR = matrix.size() - 1;
int endC = matrix[0].size() - 1;
while (a != endR + 1) //a 走到最后一行的时候b到了最后一列
{
this->printLevel(matrix,a,b,c,d,fromUp,result);
a = (b == endC ? a + 1 : a);
b = (b == endC ? b : b + 1);
d = (c == endR ? d + 1 : d);
c = (c == endR ? c : c + 1);
fromUp = !fromUp;
}
return result;
}
private:
/*打印一列*/
void printLevel(vector<vector<int>>& matrix,int a,int b,int c,int d,bool f,vector<int>& result)
{
if (f)
{
/*f = true 自上而下的打印矩阵*/
while (a != c + 1)
{
result.push_back(matrix[a][b]);
a++;
b--;
}
}
else
{
/*f = false 自下而上的打印矩阵*/
while (c != a - 1)
{
result.push_back(matrix[c][d]);
c--;
d++;
}
}
}
};
498. Diagonal Traverse的更多相关文章
- 【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对角线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 ...
- 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] Diagonal Traverse 对角线遍历
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- LeetCode Weekly Contest 18B
1. 496. Next Greater Element I 暴力的话,复杂度也就1000 * 1000 = 1e6, 在1s的时限内完全可以. 当然,有许多优化方法,利用stack维护递减序列的方法 ...
随机推荐
- Data - 【转】数据统计、数据挖掘、大数据、OLAP的区别
原文链接 数据分析 数据分析是一个大的概念,理论上任何对数据进行计算.处理从而得出一些有意义的结论的过程,都叫数据分析. 从数据本身的复杂程度.以及对数据进行处理的复杂度和深度来看,可以把数据分析分为 ...
- python笔记之列表
python中列表使用list类. 创建一个列表:list1 = [1,2,3,4]使用逗号隔开每个元素,使用方括号包含起来,创建空列表直接使用list2 = [] #!/usr/bin/env py ...
- div随着屏幕滚动而滚动
<script type="text/javascript"> $(document).ready(function () { var menuYloc = $(&qu ...
- vue-teach
编译器的工作过程 http://www.ruanyifeng.com/blog/2014/11/compiler.html DNS 原理入门 http://www.ruanyifeng.com/blo ...
- [转帖]DotNetCore跨平台~System.DrawingCore部署Linux需要注意的
DotNetCore跨平台~System.DrawingCore部署Linux需要注意的 https://www.bbsmax.com/A/QV5ZemYVJy/?tdsourcetag=s_pc ...
- Mybatis(三) 动态SQL
if + where 用法 1. if 元素来实现多条件查询 1.1 UserMapper.xml配置文件 <!--查询用户列表 (if)--> <select id="g ...
- Linx
1. 2. 2. 3. 5. Vi 猜数字 第二十个裴伯拉数字 1 1 2 3 5 8 2 3 求小于3000的裴伯拉数列 4 5 递归方式1到100 和 6 7 100 以内奇数.偶数和 8 Sss ...
- 设计模式:备忘录模式(Memento)
个人比较喜欢玩单机游戏,什么仙剑.古剑.鬼泣.使命召唤.三国无双等等一系列的游戏我都玩过(现在期待凡人修仙传),对于这些游戏除了剧情好.场面大.爽快之外,还可以随时存档,等到下次想玩了又可以从刚开始的 ...
- POJ - 1815 Friendship (最小点割集)
(点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...
- MySQL之无限级分类表设计
首先查找一下goods_cates表和table_goods_brands数据表 分别使用命令: root@localhost test>show columns from goods_cate ...