当前位置:网站首页>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();
}
}
边栏推荐
- How do novices choose the grade of investment and financial products?
- Can the long-term financial products you buy be shortened?
- SSH Remote Password free login
- Using pandas to read SQL server data table
- 正规方程、、、
- 解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
- Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
- 记录一下MySql update会锁定哪些范围的数据
- uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
- 微信小程序學習之 實現列錶渲染和條件渲染.
猜你喜欢

2.登陆退出功能开发

操作符详解

Machine learning - principal component analysis (PCA)

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

6.套餐管理业务开发

JS singleton mode

How to standardize data center infrastructure management process

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

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

Cicflowmeter source code analysis and modification to meet requirements
随机推荐
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
canvas无限扫描js特效代码
二叉树第一部分
numpy.logical_and()
uniapp 开发微信公众号,下拉框默认选中列表第一个
411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
机器学习——主成分分析(PCA)
卷妹带你学jdbc---2天冲刺Day1
Using pandas to read SQL server data table
Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
Development of anti fleeing marketing software for health products
Troubleshooting steps for Oracle pool connection request timeout
p5.js实现的炫酷交互式动画js特效
How does home office manage the data center network infrastructure?
微信小程序學習之 實現列錶渲染和條件渲染.
2021-08-17
MySQL data advanced
上升的气泡canvas破碎动画js特效
正规方程、、、
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
輸入:nums = [2,7,11,15], target = 9