leetcode:matrix zigzag traversal

1、

  Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-order.

Given a matrix:

[
  [1, 2,  3,  4],
  [5, 6,  7,  8],
  [9,10, 11, 12]
]

return [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12]

2、不懂:

  

 /**
     * @param matrix: a matrix of integers
     * @return: an array of integers
     */
    public int[] printZMatrix(int[][] matrix) {
        // write your code here
            int x, y, dx, dy, count, m, n;
            x = y = 0;
            count = 1;
            dx = -1; dy = 1;
            m = matrix.length;
            n = matrix[0].length;
            int[] z = new int[m*n];
            z[0] = matrix[0][0];
            while (count<m*n) {
                if (x+dx>=0 && y+dy>=0 && x+dx<m && y+dy<n) {
                    x += dx; y += dy;
                }
                else
                    if (dx==-1 && dy ==1) {
                        if (y+1<n) ++y; else ++x;
                        dx = 1; dy = -1;
                    }
                    else {
                        if (x+1<m) ++x; else ++y;
                        dx = -1; dy = 1;
                    }
                z[count] = matrix[x][y]; ++count;
            }
            return z;
    }
时间: 2024-11-02 23:31:18

leetcode:matrix zigzag traversal的相关文章

Matrix Zigzag Traversal(LintCode)

Matrix Zigzag Traversal Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in ZigZag-order. Have you met this question in a real interview? Yes Example Given a matrix: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12] ]

Lintcode: Matrix Zigzag Traversal

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ZigZag-order. Have you met this question in a real interview? Yes Example Given a matrix: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12] ] return [1, 2, 5, 9, 6,

lintcode 容易题:Matrix Zigzag Traversal 矩阵的之字型遍历

题目: 矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12] ] 返回 [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12] 解题: 感觉很简单,就是没有搞出来,程序来源 ,这里是先右上走,我换成先横着走就是不对了,表示不理解.另外一种方法,表示也不理解. java程序: public class Solut

Lintcode185 Matrix Zigzag Traversal solution 题解

[题目描述] Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in ZigZag-order. 给你一个包含mxn个元素的矩阵 (m行,n列), 求该矩阵的之字型遍历. [题目链接] www.lintcode.com/en/problem/matrix-zigzag-traversal/ [题目解析] Z字形走法,从左下到右上,右移或下移一位,再从右上到左下,下移或右移一位,如此往复

leetcode -day16 ZigZag Conversion

1.  ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line b

LeetCode 6 - ZigZag Conversion

原题如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHN

LeetCode 6 ZigZag Conversion 模拟 难度:0

https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y

LeetCode 6 ZigZag Conversion(规律)

题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I

leetCode 6. ZigZag Conversion 字符串 (上传费劲)

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+