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字形走法,从左下到右上,右移或下移一位,再从右上到左下,下移或右移一位,如此往复。

注意两点:

1、两个while循环必须是先走斜上的循环,再走斜下的循环;

2、两个while循环之后的平移操作,有着严格的相对顺序:斜上之后的平移,先考虑右移,再考虑下移;斜下之后的平移,先考虑下移,再考虑右移。

【参考答案】

www.jiuzhang.com/solutions/matrix-zigzag-traversal/

原文地址:https://www.cnblogs.com/foxli/p/8323417.html

时间: 2024-11-29 04:33:31

Lintcode185 Matrix Zigzag Traversal solution 题解的相关文章

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

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

Lintcode70 Binary Tree Level Order Traversal II solution 题解

[题目描述] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) [题目链接] www.lintcode.com/en/problem/binary

Lintcode28 Search a 2D Matrix solution 题解

[题目描述] Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previo

LeetCode102 Binary Tree Level Order Traversal Java题解

题解: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 解

Lintcode20 Dices Sum solution 题解

[题目描述] Throw n dices, the sum of the dices' faces is S. Given n, find the all possible value of S along with its probability. Notice:You do not care about the accuracy of the result, we will help you to output results. 扔 n 个骰子,向上面的数字之和为 S.给定 Given n,

Lintcode17 Subsets solution 题解

[题目描述] Given a set of distinct integers, return all possible subsets. Notice:Elements in a subset must be in non-descending order;The solution set must not contain duplicate subsets. 给定一个含不同整数的集合,返回其所有的子集 注意:子集中的元素排列必须是非降序的,解集必须不包含重复的子集 [题目链接] http:/