当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
面向未来的 IT 基础设施管理架构——融合云(Unified IaaS)
apr_thread使用内存之谜
组合导航精度分析
Are you hungry - Institution tree radio
TortoiseSVN小乌龟的使用
Redis 性能影响 - 异步机制和响应延迟
RADIUS Authentication Server Deployment Costs That Administrators Must Know
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
ACM模板笔记:最长不下降/上升子序列
LeetCode-498-对角线遍历
随机推荐
地理探测器Geodetector软件的下载、应用与结果解读
PostgreSQL 介绍
B. Codeforces Subsequences
睡前故事|用Bitmap与AST做一个配置化时长系统
ENVI感兴趣区ROI文件由XML格式转为ROI格式
PPT的两个实用技巧
【vulhub】MySql身份认证绕过漏洞复现(CVE-2012-2122)
DELETE:删除操作语法&使用例——《mysql 从入门到内卷再到入土》
Auto.js找图找色常用功能
Floating window in Auto.js
测试代码新的规则
力扣221题,最大正方形
力扣215题,数组中的第K个最大元素
华为路由器旁挂引流实验(使用流策略)
使用SylixOS虚拟串口,实现系统串口自由
PostgreSQL — 安装及常用命令
Auto.js中的悬浮窗
In 2021 China industrial Internet security competition (competition) in fujian province and the first industry of fujian province Internet innovation competition
Likou 221 questions, the largest square
玩转doxygen 之RT-THREAD