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.
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Solution:
class Solution(object): def findDiagonalOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ res = [] if not matrix or not matrix[0]: return res m = len(matrix) n = len(matrix[0]) scan_nums = m + n - 1 for i in range(0, scan_nums): if i % 2 == 0: x = i if i < m else m - 1 y = 0 if i < m else i - m + 1 while (x >= 0 and y < n): res.append(matrix[x][y]) x -= 1 y += 1 else: x = 0 if i < n else i - n + 1 y = i if i < n else n - 1 while (x < m and y >= 0): res.append(matrix[x][y]) x += 1 y -= 1 return res
相关推荐
网络资源拿出来与大SVG是目前最火热的图像文件格式,它的英文全称为Scalable Vector Graphics,意思为可缩放的矢量图形,它是基于XML,由W3C联盟进行开发。SVG可以实现无损缩放,在性能上要比其它格式的图片更好家...
matlab开发-diagonal.zip.zip
IMX290_291LQR_Flyer.Diagonal 6.46mm square pixel array color CMOS image sensor
var diagonal = d3.svg.diagonal() 将对角线附加到画布上,并为其提供一些基本属性 canvas.append("path") .attr("fill", "none") .attr("stroke", "black") .attr("d", diagonal); 添加对角线的源坐标和...
aabb-tree:MATLAB的d维aabb-tree实现
matlab开发-diagonal。返回具有矩阵主对角线的向量。类似于内置功能diag
Contents Page 0 Introduction.................................................................................................................................................................
追赶法求三对角方程组 参数:a为下次对角元素向量, b是主对角元素向量,c为上次对角元素向量,f为右端向量
Current bugs: Add Poisson-disc sampling ...Diagonal lines on some devices - auto-calibrate bitmap offset using a screenshot. Filter does not cover navigation buttons fully when the keyboard is shown.
6 Block-Diagonal Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 7 Spectral Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Diagonal Matrices ................................................................................ 32 Triangular Matrices .................................................................................
squint:gravity="right" />java:DiagonalView diagonalView = (DiagonalView) findViewById(R.id.diagonal); // to set image from resources diagonalView.setImageSource(R.drawable.your_image...
python路径查找 python 2和3的寻路算法 当前,此库中捆绑了7个路径查找器... diagonal_movement import DiagonalMovement from pathfinding . core . grid import Grid from pathfinding . finder . a_star import ASta
Christopher M. Bishop 1 Introduction 1 1.1 Example: Polynomial Curve Fitting . . . . . . ....1.2 Probability Theory ....1.2.1 Probability densities ....1.2.2 Expectations and covariances ....1.2.3 Bayesian ...
J Diagonal Equivalence of Nonnegative Matrices . . . 76 3 Schur-Convex Functions 79 A Characterization of Schur-Convex Functions . . . . 80 B Compositions Involving Schur-Convex Functions . . 88 C ...
Matrix Transpose, Diagonal Elements, and LU Decomposition.
A diagonal argument is used to show that there are questions about the actions of Turing machines which cannot be answered by any machine. By formalizing the action of Turing machines in the lower ...
2.2.1 Diagonal ply tyres......Page 101 2.2.4 Height-to-width ratio......Page 105 2.2.5 Tyre dimensions and markings......Page 109 2.2.6 Tyre load capacities and inflation pressures......Page 113 2.2.8...
Drawing a diagonal line. diagonal_line.py Drawing a square. square.py Drawing a horizontal rectangle. horizonal_rectangle.py Drawing a vertical rectangle. horizonal_rectangle.py Drawing random ...
用回溯法(Back Tracking Method)求解對角線數獨的答案,運行Solve_Sudoku_diagonal.c,可以修改其中的 InputSudoku的二維數組,找到其中一個答案便停止。