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

 public class Solution {
/**
* @param matrix: a matrix of integers
* @return: an array of integers
*/
public int[] printZMatrix(int[][] matrix) {
// write your code here
if (matrix==null || matrix.length==0 || matrix[0].length==0) return null;
int m = matrix.length;
int n = matrix[0].length;
int count = m*n;
int[] res = new int[count];
int x=0, y=0;
for (int i=0; i<count;) {
while (x>=0 && y<n) {
res[i++] = matrix[x--][y++];
}
if (i == count) break;
if (x<0 && y<n) {
x++;
}
else {
x = x+2;
y = y-1;
}
while (x<m && y>=0) {
res[i++] = matrix[x++][y--];
}
if (i == count) break;
if (x<m && y<0) {
y++;
}
else {
x = x-1;
y = y+2;
}
}
return res;
}
}

Lintcode: Matrix Zigzag Traversal的更多相关文章

  1. Matrix Zigzag Traversal(LintCode)

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

  2. [OJ] Matrix Zigzag Traversal

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

  3. lintcode:Matrix Zigzag Traversal 矩阵的之字型遍历

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

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  6. 别再埋头刷LeetCode之:北美算法面试的题目分类,按类型和规律刷题,事半功倍

    算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...

  7. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  8. 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...

  9. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

随机推荐

  1. 微博app中常用正则表达式

    /* weibo.app 里面的正则,有兴趣的可以参考下: HTTP链接 (例如 http://www.weibo.com ): ([hH]ttp[s]{0,1})://[a-zA-Z0-9\.\-] ...

  2. ArcGIS for Sever 10.1 服务迁移与恢复

    === 声明:以下内容本是自己写给单位内部同事的参考手册,但是被传到百度文库中.陆续有用户就这方面的问题,通过电话,邮件等方式联系我.首先,感到荣幸.其次是,由于本人当时测试和编写的时候,由于仓促,可 ...

  3. 在Delphi下,如何使ShowMessage中的按钮中文化

    如果你使用messagedlg(对showmessage也适用)可以汉化定义按钮caption的常量,具体操作步骤如下: 1.   打开文件consts.pas(在Delphi安装目录的/source ...

  4. SQLQuery 实现别名映射

    public List getQueryList(Map paramMap, int start, int maxResults) throws DataAccessException, Hibern ...

  5. [转]C#之反射

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...

  6. Bootstrap 进度条媒体对象和 Well 组件

    一.Well 组件 这个组件可以实现简单的嵌入效果. //嵌入效果 <div class="well"> Bootstrap </div> //有 lg 和 ...

  7. 回调的代理(delegate)实现

    1.CoreManage.h #import <Foundation/Foundation.h> @protocol SampleProtocol; //声明核心类的属性和方法 @inte ...

  8. Nagios Looking Glass 本地文件包含漏洞

    漏洞名称: Nagios Looking Glass 本地文件包含漏洞 CNNVD编号: CNNVD-201310-682 发布时间: 2013-10-31 更新时间: 2013-10-31 危害等级 ...

  9. magento 切换数据库,使用不同数据库

    1. 在app/etc/local.xml 中,添加新的数据库选项 <?xml version="1.0"?> <config> <global> ...

  10. http请求及模拟浏览器发送http请求

    /**    *测试新增图片   * @throws IOException    * @throws HttpException    * @throws SAXException    * @th ...