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维护递减序列的方法 ...
随机推荐
- dockerfile中配置时区
https://www.cnblogs.com/kevingrace/p/5570597.html #设置时区RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai ...
- [转] Maven 从命令行获取项目的版本号
[From]https://blog.soebes.de/blog/2018/06/09/help-plugin/ I bet you have been faced with the situati ...
- Sqlserver实现故障转移 — sqlserver镜像备份实现故障转移(3)
目的:在已经加域的计算机上安装sqlserver2012,并配置数据库镜像实时同步,并实现故障转移. 在数据库层面实现故障自动转移后,应用程序里改怎么写数据库连接呢?其实使用ADO.NET或者SQL ...
- Day03:日期操作 / 集合框架(上)
日期操作 Java中的时间 · Java中的时间使用标准类库的Date类表示,是用距离一个固定时间点的毫秒数(可正可负,long类型)表达一个特定的时间点: · 固定的时间点叫纪元(epoch),是U ...
- Web Components的概念和用法
参考链接:https://developer.mozilla.org/zh-CN/docs/Web/Web_Components
- 协程,纤程(Fiber),或者绿色线程(GreenThread)
纤程(Fiber),或者绿色线程(GreenThread) 面试官:你知道协程吗? 你:订机票的那个吗,我常用. 面试官:行,你先回去吧,到时候电话联系 ........ 很尴尬,但是事实是,很大一部 ...
- TOEFL | 听力题型
通常是2个对话,4个讲座,但可能会有加试: 2~3 conversations 5 Questions/Each 4~6 lectures 6 Questions/Each 正 ...
- make j* make j4 make j8 区别
转载: make -j4是什么意思看书上说1) make(1)只衍生一个作业//作业是什么意思?make(1) 是不是就是make的意思?2) 在双处理器上make -j4,难道是让每个处理器跑两个作 ...
- RabbitMQ 示例-生产者-消费者-direct-topic-fanout
这是生产者和消费者2个项目, 包含 direct,topic,fanout模式下的消费,springboot + rabbitmq 代码地址:https://github.com/duende99/R ...
- VMware Ubuntu18.04 安装及配置笔记
安装Ubuntu 下载虚拟机VMware 下载镜像Ubuntu 过程略, 参考 https://zhuanlan.zhihu.com/p/38797088 Ubuntu配置 特别提示: 在Ubuntu ...