当前位置:网站首页>LeetCode-498 - Diagonal Traversal
LeetCode-498 - Diagonal Traversal
2022-08-10 21:28:00 【z754916067】
题目
思路
- 直接遍历就行.
代码
public int[] findDiagonalOrder(int[][] mat) {
int[] ans = new int[mat.length * mat[0].length];
int index = 0;
//在matIt traverses the rows and columns inside
int row=0;
int col=0;
//Top right or bottom left 右上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++;
//move right first
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++;
//Move down first
if(row+1<mat.length) row++;
else col++;
flag=true;
}
}
return ans;
}
边栏推荐
猜你喜欢
玩转doxygen 之RT-THREAD
使用SylixOS虚拟串口,实现系统串口自由
数字化转型:如何引导创新领导者
RADIUS Authentication Server Deployment Costs That Administrators Must Know
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展
石油化工行业商业供应链管理系统:标准化供应商管理,优化企业供应链采购流程
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
力扣221题,最大正方形
内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布
随机推荐
Huawei router clock near the drainage experiment (using stream strategy)
饿了么-机构树单选
这些不可不知的JVM知识,我都用思维导图整理好了
Redis命令手册
Date picker component (restrict year to set only displayed months)
【实用软件】【VSCode】使用技巧大全(持续更新)
用汇编带你看Golang里到底有没有值类型、引用类型
LeetCode-36-Binary search tree and doubly linked list
石油化工行业商业供应链管理系统:标准化供应商管理,优化企业供应链采购流程
ES6中的for...in/of的使用
PostgreSQL — 安装及常用命令
Are you hungry - Institution tree radio
基于Pix4Dmapper的运动结构恢复法无人机影像三维模型重建
【vulhub】MySql身份认证绕过漏洞复现(CVE-2012-2122)
ArcMap时间滑块功能动态显示图层数据并生成视频或动图
ArcPy读取Excel时序数据、批量反距离加权IDW插值与掩膜
Future-oriented IT infrastructure management architecture - Unified IaaS
变量和它的特性——《mysql 从入门到内卷再到入土》
论配置化系统的配置
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.