题目:

给你一个包含 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 Solution {
/**
* @param matrix: a matrix of integers
* @return: an array of integers
*/
public int[] printZMatrix(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return null;
int count = matrix.length * matrix[0].length;
int[] array = new int[count];
int r = 0, c = 0;
array[0] = matrix[0][0];
for (int i = 1; i < count; ) {
//斜上走到顶
while(i < count && r - 1 >= 0 && c + 1 < matrix[0].length) {
array[i++] = matrix[--r][++c];
}
//横右走一步,不可横右走时竖下走一步
if (i < count && c + 1 < matrix[0].length) {
array[i++] = matrix[r][++c];
} else if (i < count && r + 1 < matrix.length) {
array[i++] = matrix[++r][c];
}
//斜下走到底
while(i < count && r + 1 < matrix.length && c - 1 >= 0) {
array[i++] = matrix[++r][--c];
}
//竖下走一步,不可竖下走时横右走一步
if (i < count && r + 1 < matrix.length) {
array[i++] = matrix[++r][c];
} else if (i < count && c + 1 < matrix[0].length) {
array[i++] = matrix[r][++c];
}
}
return array;
} }

总耗时: 2001 ms

Python程序:

lintcode:Matrix Zigzag Traversal 矩阵的之字型遍历的更多相关文章

  1. Lintcode: Matrix Zigzag Traversal

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

  2. lintcode-185-矩阵的之字型遍历

    185-矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9 ...

  3. Matrix Zigzag Traversal(LintCode)

    Matrix Zigzag Traversal Given a matrix of m x n elements (m rows, ncolumns), return all elements of ...

  4. [OJ] Matrix Zigzag Traversal

    LintCode #46. Matrix Zigzag Traversal (Easy) class Solution { public: vector<int> printZMatrix ...

  5. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  6. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  7. 算法:Z字型(Zigzag)编排

    问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0) ...

  8. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

随机推荐

  1. 最初程序员的思维“修炼”之四——Android平台开发的“强制关闭”解决思路

    我和我的朋友参加一个比赛——物联网应用技能大赛,这个大赛所要求的技能有,硬件技术,Android平台开发技术,.NET平台开发技术,所以这是一个团队合作的比赛,基本上没有人能同时掌握这三种技术(在校生 ...

  2. 【Qt】Qt环境搭建(Qt Creator)【转】

    简述 上一节中介绍了如何进行Qt和Visual Studio的下载安装,随后演示了如何将Qt集成到Visual Studio中,并完成了我们第一个Qt小程序-Hello World.下面主要讲解如何利 ...

  3. 51nod 1021 石头归并

    1021 石子归并 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合 ...

  4. 用MySQL log调试程序

    打开my.ini文件 在[mysqld]的下面加上log = c:/mysql_query.log.txt重启mysql 以后你用可以用editplus查看你运行的sql了,不用在程序里一句句的用lo ...

  5. Laravel 5 基础(一)- Laravel入门和新建项目

    此系列文章是 laracasts.com 中的入门系列视频的笔记,我做了一些修改,可以参考此系列文章来学习 Laravel 5.原视频作者是 Jeffrey Way, 在此感谢.本人使用的系统是Mac ...

  6. ASP.NET Core 1.0

    .NET Core dotnet 命令大全:http://www.cnblogs.com/linezero/p/dotnet.html http://www.cnblogs.com/Wddpct/p/ ...

  7. MATLAB plot画线的颜色设定

    plot中画线的颜色通常是八种: 标记符    颜色r          红g          绿b          蓝c          蓝绿m          紫红y          黄 ...

  8. postgreSQL数据库(索引、视图)

    索引的含义与特点 索引是一个单独的.存储在磁盘上的数据库结构,它们包含对数据所有记录的引用指针,postgresql列类型都可以被索引,对相关列索引是提高查询操作效率的最佳途径.例如,查询select ...

  9. 【Ibatis】总结各种使用技巧

    [Ibatis]总结各种使用技巧 <alias> <typeAlias alias="YintaiMobile_FreeData_Model" type=&quo ...

  10. iOS 进阶 第十天(0410)

    0410 在tableViewCell之间添加一根线,通栏 iOS应用数据存储的常用方式 plist存储文件 plist读取文件 下面是plist存储读取的图解: 注意:plist只能存储常见的属性. ...