498. Diagonal Traverse 对角线遍历矩阵

Given a matrix of M x N elements (M rows, N columns), return all
elements of the matrix in diagonal order as shown in the below image.

Example:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output:  [1,2,4,7,5,3,6,8,9]
Explanation:

Note:

  1. The total number of elements of the given matrix will not exceed 10,000.
  1. class Solution:
  2. def findDiagonalOrder(self, matrix):
  3. """
  4. :type matrix: List[List[int]]
  5. :rtype: List[int]
  6. """
  7. if not matrix or not matrix[0]:
  8. return []
  9. res = []
  10. isRe = 1
  11. def addLine(row, col, isRe):
  12. line = []
  13. while row < len(matrix) and col >= 0:
  14. line.append(matrix[row][col])
  15. row += 1
  16. col -= 1
  17. l = len(res)
  18. if isRe:
  19. res[l:l] = line[::-1]
  20. else:
  21. res[l:l] = line
  22. for i in range(len(matrix[0])):
  23. addLine(0, i, isRe)
  24. isRe ^= 1
  25. for i in range(1, len(matrix)):
  26. addLine(i, len(matrix[i]) - 1, isRe)
  27. isRe ^= 1
  28. return res

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8419850.html

时间: 2024-07-30 12:14:52

498. Diagonal Traverse 对角线遍历矩阵的相关文章

【LeetCode】4.Array and String — Diagonal Traverse 对角线遍历

Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation: Note: The total

498. Diagonal Traverse对角线z型traverse

[抄题]: Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation: [暴力解法]: 时间

LeetCode 498. Diagonal Traverse

原题链接在这里:https://leetcode.com/problems/diagonal-traverse/ 题目: Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9

498. Diagonal Traverse

1 class Solution { 2 public int[] findDiagonalOrder(int[][] matrix) { 3 if(matrix.length == 0) return new int[0]; 4 int row = matrix.length; 5 int col = matrix[0].length; 6 int i = 0, j = 0; 7 List<Integer> list = new ArrayList<>(); 8 int flag

对角线遍历

题目:给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 说明: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,4,7,5,3,6,8,9] 思路:观察规律,主要思考这个矩阵是怎么遍历的,遍历方向如何,研究可得,遍历方向由元素所在行列和决定,为偶数向上,奇数向下. 所以,即先创建空列表,然后append迭代添加即可. 还是自己多画图找规律. class So

OpenCV快速遍历矩阵元素方法

OpenCV中Mat矩阵data数据的存储方式和二维数组不一致,二维数组按照行优先的顺序依次存储,而Mat中还有一个标示行步进的变量Step.使用Mat.ptr<DataTyte>(row) 行指针的方式定位到每一行,可快速遍历矩阵.例程如下: 1 std::cout << "The inverse matrix of K is:" << std::endl; 2 for(int i=0;i<3;i++) 3 { 4 float* data =

某比赛小记5- 螺旋遍历矩阵

题目:给定一个二维矩阵,从[0][0]开始向右,按顺时针遍历全部数据,比如[[a,b][c,d]],遍历顺序就是a,b,d,c.最后将遍历的元素用逗号连接,打印整个遍历结果.给定二维矩阵见文件. 题解:本题思路很容易设计,就是每次访问越界或者是访问到已经访问的元素就向右转向,当右边没有路了则结束.难点主要是边界条件的处理. python版本: m = [给定矩阵] #利用numpy构造二维全0数组 # tagnp = np.zeros(32*32) # tagnp = tagnp.reshape

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.