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, 3, 4, 7, 10, 11, 8, 12]

Tags Expand  

先斜上走到顶,再斜下走到底,直到计数器满, 写的时候老是fail,才发现14行for循环i不需要++,循环里面自己加了

注意corner cases, 以斜上为例

如果是

1,2

5,6

9,10

中6的这种情况,下一个点是10,则x = x+2, y=y-1

如果是1这种情况, 下一个点是2,则只需x = x+1

 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         // write your code here
 8         if (matrix==null || matrix.length==0 || matrix[0].length==0) return null;
 9         int m = matrix.length;
10         int n = matrix[0].length;
11         int count = m*n;
12         int[] res = new int[count];
13         int x=0, y=0;
14         for (int i=0; i<count;) {
15             while (x>=0 && y<n) {
16                 res[i++] = matrix[x--][y++];
17             }
18             if (i == count) break;
19             if (x<0 && y<n) {
20                 x++;
21             }
22             else {
23                 x = x+2;
24                 y = y-1;
25             }
26             while (x<m && y>=0) {
27                 res[i++] = matrix[x++][y--];
28             }
29             if (i == count) break;
30             if (x<m && y<0) {
31                 y++;
32             }
33             else {
34                 x = x-1;
35                 y = y+2;
36             }
37         }
38         return res;
39     }
40 }
时间: 2024-10-05 23:09:15

Lintcode: 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 矩阵的之字型遍历

题目: 矩阵的之字型遍历 给你一个包含 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

[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    /  \  

Leetcode 103. Binary Tree Zigzag Level Order Traversal

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>>