当前位置:网站首页>LeetCode-498-对角线遍历
LeetCode-498-对角线遍历
2022-08-10 20:54:00 【z754916067】
题目


思路
- 直接遍历就行。
代码
public int[] findDiagonalOrder(int[][] mat) {
int[] ans = new int[mat.length * mat[0].length];
int index = 0;
//在mat里面遍历的行和列
int row=0;
int col=0;
//右上还是左下 右上true 左下false
boolean flag = true;
while (row<mat.length && col<mat[0].length){
if(flag){
while (row>=0 && col<mat[0].length){
ans[index++] = mat[row--][col++];
}
col--;
row++;
//首先右移
if(col+1<mat[0].length) col++;
else row++;
flag=false;
}else {
while(row<mat.length && col>=0){
ans[index++] = mat[row++][col--];
}
row--;
col++;
//首先下移
if(row+1<mat.length) row++;
else col++;
flag=true;
}
}
return ans;
}
边栏推荐
猜你喜欢

TortoiseSVN小乌龟的使用

JS中的filter、map、reduce

【Windows】你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问,这些策略可帮助保护你的电脑

化学制品制造业数智化供应链管理系统:建立端到端供应链采购一体化平台

参天生长大模型:昇腾AI如何强壮模型开发与创新之根?

Detailed explanation and use of each module of ansible

win10 xbox录屏功能不能录声音怎么办
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU

【一致性hash】负载均衡器分发请求

npm‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
随机推荐
关于 NFT 版权保护的争议
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
日期选择器组件(限制年份 设定仅展示的月份)
[Golang]如何优雅管理系统中的几十个UDF(API)
2021DozerCTF
MATLAB神经网络拟合工具箱Neural Net Fitting使用方法
ArcMap创建镶嵌数据集、导入栅格图像并修改像元数值显示范围
[Golang]从0到1写一个web服务(上)
How to submit a PR?【OpenHarmony Growth Plan】【OpenHarmony Open Source Community】
论文解读(g-U-Nets)《Graph U-Nets》
姜还是老的辣,看看老战哥的老底儿和严谨劲儿
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展
APP application related instructions in Auto.js
Before implementing MES management system, these three questions to consider
用示波器揭示以太网传输机制
Introduction to PostgreSQL
实施MES管理系统前,这三个问题要考虑好
双 TL431 级联振荡器
查询:复杂查询的语法和使用例——《mysql 从入门到内卷再到入土》
DDL:CREATE 创建数据库——《mysql 从入门到内卷再到入土》