首页 > 代码库 > leetcode498
leetcode498
public class Solution { public int[] FindDiagonalOrder(int[,] matrix) { var row = matrix.GetLength(0); var col = matrix.GetLength(1); var upBound = row - 1 + col - 1; var ary = new int[row * col]; int i = 0; int j = 0; var index = 0; if (row == 1) { for (int m = 0; m < col; m++) { ary[index++] = matrix[0, m]; } return ary; } if (col == 1) { for (int m = 0; m < row; m++) { ary[index++] = matrix[m, 0]; } return ary; } var direction = true;// for (int k = 0; k <= upBound; k++)//对角线上的i,j之和 { if (i == 0 && j == 0) { ary[index++] = matrix[i, j]; j = 1; continue; } //结束点 if (i == row - 1 && j == col - 1) { ary[index++] = matrix[i, j]; break; } var EndJ = Math.Min(i, col - 1);//0 var EndI = Math.Min(j, row - 1);//1 while (i + j == k) { ary[index++] = matrix[i, j]; if ((direction && (j == 0 || i == row - 1)) || (!direction && (i == 0 || j == col - 1))) { if (direction) { i++; if (i > row - 1) { i = row - 1; j++; if (j > col - 1) { j = col - 1; } } } else { j++; if (j > col - 1) { j = col - 1; i++; if (i > row - 1) { i = row - 1; } } } direction = !direction; continue; } if (direction) { i++; if (i > row - 1) { i = row - 1; } j--; if (j < 0) { j = 0; } } else { j++; if (j > col - 1) { j = col - 1; } i--; if (i < 0) { i = 0; } } } } return ary; } }
https://leetcode.com/problems/diagonal-traverse/#/description
leetcode498
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。