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:

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么控制方向:由于每次只走一格,所以用xy+-d即可,d=1

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 为了保证第一个数一样,都是先直接添加后再处理

result[i] = matrix[row][col];
row -= d;
col += d;

[二刷]:

  1. 为了避免处理>边界时顺便把<边界处理了,<边界的小情况要写在后面

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

换方向用d = -d来控制

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

public class Solution {
    public int[] findDiagonalOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0) return new int[0];
        int m = matrix.length, n = matrix[0].length;

        int[] result = new int[m * n];
        int row = 0, col = 0, d = 1;

        //for loop: add to result, expand, handle corner cases
        for (int i = 0; i < m * n; i++) {
            result[i] = matrix[row][col];

            row -= d;
            col += d;

            if (row >= m) {row = m - 1; col += 2; d = -d;}
            if (col >= n) {col = n - 1; row += 2; d = -d;}
            if (row < 0) {row = 0; d = -d;}
            if (col < 0) {col = 0; d = -d;}
        }

        return result;
    }
}

原文地址:https://www.cnblogs.com/immiao0319/p/9462047.html

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

498. Diagonal Traverse对角线z型traverse的相关文章

LeetCode 6 ZigZag Conversion(Z型转换)

翻译 字符串"PAYPALISHIRING"通过一个给定的行数写成如下这种Z型模式: P A H N A P L S I I G Y I R 然后一行一行的读取:"PAHNAPLSIIGYIR" 写代码读入一个字符串并通过给定的行数做这个转换: string convert(string text, int nRows); 调用convert("PAYPALISHIRING", 3),应该返回"PAHNAPLSIIGYIR".

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: The total

【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

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

“爱奇艺”才能赢 众创空间的Z型成功路线

回归奇葩,其实并不是要奇技淫巧.招式蛊惑,而是在自己要孵化的那些创客所在的垂直领域里,先期成为一朵奇葩. 文/张书乐 日前,株洲日报再一次走进了经历了入驻率不高.项目成活率低.缺少盈利模式等"成长的烦恼"之后,正在逐步稳定的几家株洲市的众创空间,一番探寻之下,发现它们的心声大多是:努力寻找稳定可靠盈利模式是当前重点. 怎么破,每一个众创空间的平台方都给出了一些大致的方向,如深耕文创.专注无人机.强化培训.引入人才等.不过在笔者看来,这些招式看似有用,但却并不创新. 或许,经历了2年多热

字符串的z型转换

class Solution(object): def convert(self, s, numRows): if numRows==1: return s res = ['' for _ in range(numRows)] # 周期 T = numRows + numRows -2 for i in range(len(s)): t_num = i%T temp = t_num if t_num<numRows else numRows-(t_num)%numRows-2 res[temp]

算法:Z字型(Zigzag)编排

问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0)位置开始水平向右遍历,当到达(0, 1)时沿着反对角线方向左下遍历(利用一个变量控制左下右上方向),内层循环一直遍历到碰到边缘时row++,方向改为右上,沿着反对角线碰到矩阵上边缘时col++,方向变为左下遍历,知道上半部分(包括反对角线遍历完):遍历完半个矩阵(可能是子方矩阵)后,根据当前 row

Option Traverse

package option object Traverse { def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = a match { case Nil => Some(Nil) case h :: t => Map2.map2(f(h), traverse(t)(f))(_ :: _) } def main(args: Array[String]): Unit = { val l = List(1,