当前位置:网站首页>leetCode-498: 對角線遍曆
leetCode-498: 對角線遍曆
2022-06-24 10:14:00 【文醜顏不良啊】
題目描述
給你一個大小為 m x n 的矩陣 mat ,請以對角線遍曆的順序,用一個數組返回這個矩陣中的所有元素。
示例
示例 1:
輸入:nums = [2,7,11,15], target = 9
輸出:[0,1]
解釋:因為 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
輸入:mat = [[1,2],[3,4]]
輸出:[1,2,3,4]
解題過程
思路及步驟
(1)m * n 的二維矩陣, 總共有 m + n - 1 條對角線, 相鄰的對角線的遍曆方向不同;
(2)設對角線從上到下的編號為 [0,m + n − 2]。當 i 為偶數時, 遍曆方向為從左下向右上遍曆;當 i 為奇數時,遍曆方向為從右上向左下遍曆;
(3)當第 i 條對角線為從左下向右上遍曆時, 即 i 為偶數時, 每次行索引减 1, 列索引加 1, 直到矩陣的邊緣為止;
當 i < m 時, 此時對角線遍曆的起點比特置為 (i, 0);
當 i ≥ m 時,此時對角線遍曆的起點比特置為 (m − 1, i − m + 1);
(4)當第 i 條對角線為從右上向左下遍曆時, 即 i 為奇數時, 每次行索引加 1, 列索引减 1, 直到矩陣的邊緣為止;
當 i < n 時, 此時對角線遍曆的起點比特置為 (0, i);
當 i ≥ n 時,則此時對角線遍曆的起點比特置為 (i − n + 1, n − 1);
代碼展示
public class FindDiagonalOrder {
/** * 官方解答: * m * n 的二維矩陣, 總共有 m + n - 1 條對角線, 相鄰的對角線的遍曆方向不同 * 設對角線從上到下的編號為 [0,m + n − 2] * 當 i 為偶數時, 遍曆方向為從左下向右上遍曆; * 當 i 為奇數時,遍曆方向為從右上向左下遍曆; * * 當第 i 條對角線為從左下向右上遍曆時, 即 i 為偶數時, 每次行索引减 1, 列索引加 1, 直到矩陣的邊緣為止; * 當 i < m 時, 此時對角線遍曆的起點比特置為 (i, 0); * 當 i ≥ m 時,此時對角線遍曆的起點比特置為 (m − 1, i − m + 1); * * 當第 i 條對角線為從右上向左下遍曆時, 即 i 為奇數時, 每次行索引加 1, 列索引减 1, 直到矩陣的邊緣為止; * 當 i < n 時, 此時對角線遍曆的起點比特置為 (0, i); * 當 i ≥ n 時,則此時對角線遍曆的起點比特置為 (i − n + 1, n − 1); **/
public int[] findDiagonalOrder(int[][] mat) {
// 行
int m = mat.length;
// 列
int n = mat[0].length;
int[] result = new int[m * n];
int index = 0;
for (int i = 0; i < m + n - 1; i++) {
if (i % 2 == 0) {
// 第偶數條對角線
int row = 0;
int line = 0;
if (i < m) {
row = i;
}
if (i >= m) {
row = m - 1;
line = i - m + 1;
}
while (row >= 0 && line < n) {
result[index] = mat[row][line];
row--;
line++;
index++;
}
} else {
// 第奇數條對角線
int row = 0;
int line = 0;
if (i < n) {
line = i;
}
if (i >= n) {
row = i - n + 1;
line = n - 1;
}
while (row < m && line >= 0) {
result[index] = mat[row][line];
row++;
line--;
index++;
}
}
}
return result;
}
public static void main(String[] args) {
int[][] mat = {
{
1,2,3},
{
4,5,6},
{
7,8,9}};
int[] result = new FindDiagonalOrder().findDiagonalOrder(mat);
for (int i = 0; i < result.length; i++) {
System.out.printf("%2d", result[i]);
}
System.out.println();
}
}
边栏推荐
- Cicflowmeter source code analysis and modification to meet requirements
- p5.js实现的炫酷交互式动画js特效
- El table Click to add row style
- canvas管道动画js特效
- 416 binary tree (first, middle and last order traversal iteration method)
- Cookie encryption 4 RPC method determines cookie encryption
- SSH Remote Password free login
- oracle池式连接请求超时问题排查步骤
- 100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
- How large and medium-sized enterprises build their own monitoring system
猜你喜欢

Cicflowmeter source code analysis and modification to meet requirements

CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance

微信小程序學習之 實現列錶渲染和條件渲染.

形状变化loader加载jsjs特效代码

canvas掉落的小球重力js特效动画

解决Deprecated: Methods with the same name as their class will not be constructors in报错方案

上升的气泡canvas破碎动画js特效

物联网?快来看 Arduino 上云啦

使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?

Indexeddb local storage, homepage optimization
随机推荐
Operator details
操作符详解
Cookie encryption 4 RPC method determines cookie encryption
Groovy obtains Jenkins credentials through withcredentials
小程序 rich-text中图片点击放大与自适应大小问题
分布式 | 如何与 DBLE 进行“秘密通话”
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
MYSQL数据高级
GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
Yolov6: the fast and accurate target detection framework is open source
uniapp 开发微信公众号,下拉框默认选中列表第一个
小程序学习之获取用户信息(getUserProfile and getUserInfo)
numpy.logical_or
How to standardize data center infrastructure management process
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
numpy.logical_and()
读取csv(tsv)文件出错
CICFlowMeter源码分析以及为满足需求而进行的修改
Distributed | how to make "secret calls" with dble
Machine learning - principal component analysis (PCA)
輸入:nums = [2,7,11,15], target = 9