当前位置:网站首页>LeetCode_模拟_中等_498.对角线遍历
LeetCode_模拟_中等_498.对角线遍历
2022-08-04 14:31:00 【小城老街】
1.题目
给你一个大小为 m x n 的矩阵 mat ,请以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
示例 1:
输入:mat = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,4,7,5,3,6,8,9]
示例 2:
输入:mat = [[1,2],[3,4]]
输出:[1,2,3,4]
提示:
m == mat.length
n == mat[i].length
1 <= m, n <= 104
1 <= m * n <= 104
-105 <= mat[i][j] <= 105
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/diagonal-traverse
2.思路
(1)模拟
① 首先,分析题目可知,在整个遍历过程中,只有两种遍历方向:右上方和左下方;
② 在朝右上方遍历时,如果遍历到边界(例如示例 1 中的元素 1 和 3):此时先判断当前元素的右侧相邻元素的位置是否越界,如果未越界,则遍历该元素,并且切换遍历方向;如果越界,则选择当前元素的下侧相邻元素,同时也要切换遍历方向。
③ 在朝左下方遍历时,其步骤与朝右上方遍历类似,此处不再赘述。
④ 需要注意的是,由于在遍历过程中遍历方向有所不同,所以我们可能通过 boolean 变量 flag 来表示遍历方向,例如 flag = true 表示方向为右上方。此外,该方法的时间复杂度为 O(m * n)。
3.代码实现(Java)
//思路1————模拟
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
// m 行 n 列的矩阵
int m = mat.length;
int n = mat[0].length;
// res 保存最终的结果
int[] res = new int[m * n];
/* 遍历方向只有两种:右上方和左下方 刚开始的方向是右上方,将 flag 设置为 true 那么 flag = false 表示方向为左下方 */
boolean flag = true;
//从矩阵的左上角第一个元素开始遍历
int i = 0, j = 0;
//以对角线遍历方式开始遍历矩阵
for (int k = 0; k < m * n; k++) {
res[k] = mat[i][j];
//右上方
if (flag) {
//下一个元素的位置
int x = i - 1, y = j + 1;
//如果 x、y 超过了边界,则需要变换方向
if (x < 0 || y >= n) {
// (1) 下一个元素是右边相邻元素
x = i;
y = j + 1;
if (x < 0 || y >= n) {
// (2) 下一个元素是下边相邻元素
x = i + 1;
y = j;
}
//变换方向
flag = false;
}
// i、j 替代下一个元素的位置
i = x;
j = y;
} else {
//左下方,这里的处理与右上方类似
int x = i + 1, y = j - 1;
//判断边界
if (x >= m || y < 0) {
x = i + 1;
y = j;
if (x >= m || y < 0) {
x = i;
y = j + 1;
}
//变换方向
flag = true;
}
i = x;
j = y;
}
}
return res;
}
}
边栏推荐
- LCP 06. 拿硬币-遍历
- Notes for xpath getting node with namespace
- 本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
- 集合划分差最小问题(01背包)
- License server system does not support this version of this feature
- Centos7 install mysql version rapidly
- [Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
- 基于 Next.js实现在线Excel
- X射线掠入射聚焦反射镜
- 【模型部署与业务落地】基于量化芯片的损失分析
猜你喜欢

Qt的QItemDelegate使用

Almost all known protein structures in the world are open sourced by DeepMind

开发者独立搭建一个跨模态搜索应用有多难?

eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)

输入输出流总结

理论篇1:深度学习之----LetNet模型详解

本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋

小 P 周刊 Vol.13

Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases

用了TCP协议,就一定不会丢包吗?
随机推荐
Redis 复习计划 - Redis主从数据一致性和哨兵机制
第十六章 源代码文件 REST API 教程(一)
世间几乎所有已知蛋白质结构,都被DeepMind开源了
MySQL【窗口函数】【共用表表达式】
Hangzhou electric the competition team arrangement (ACM)
【剑指offer59】队列的最大值
How to automatically renew the token after it expires?
Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
OAID是什么
我爱七夕哈哈哈
第六届未来网络发展大会,即将开幕!
leetcode:253. 至少需要多少间会议室
Technology sharing | Mini program realizes audio and video calls
The Internet of things application development trend
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
华为手机切换屏幕效果_华为p40页面切换效果怎么换
CF1527D MEX Tree(mex&树&容斥)
Problem solving-->Online OJ (18)
Crawler - action chain, xpath, coding platform use
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source