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]
]

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

 1 public class Solution {
 2     /**
 3      * @param matrix: a matrix of integers
 4      * @return: an array of integers
 5      */
 6     public int[] printZMatrix(int[][] matrix) {
 7         int m = matrix.length;
 8         int n = matrix[0].length;
 9         int l = matrix.length*matrix[0].length;
10         int[] result = new int[l];
11
12         int d = -1;
13         int x = 0;
14         int y = 0;
15         for(int i=0;i<l;i++) {
16             result[i] = matrix[x][y];
17
18             if(x+d < 0 && y < n-1 || x+d >= m && y >= 0) {
19                 d = -d;
20                 y++;
21             }else {
22                 if(y-d < 0 && x < m-1 || y-d >= n && x >= 0) {
23                     d = -d;
24                     x++;
25                 }else {
26                     x = x + d;
27                     y = y - d;
28                 }
29             }
30         }
31
32         return result;
33     }
34 }

时间: 2024-11-16 15:17:41

Matrix Zigzag Traversal(LintCode)的相关文章

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: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 mat

2 - Binary Search &amp; LogN Algorithm

254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 28. Search a 2D Matrix https://www.lintcode.com/problem/search-a-2d-matrix/description?_from=ladder&&fromId=1 思路1: 1. find the row index, the last

[Lintcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). Example Given binary tree {3,9,20,#,#,1

Lintcode 71 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). Example Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigzag l

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \