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. a compromise between lock overhead and data safety

    High Performance My SQL  THIRD EDITION A locking strategy is a compromise between lock overhead and ...

  2. spark之combineByKey

    combineByKey def combineByKey[C](createCombiner: (V) => C, mergeValue: (C, V) => C, mergeCombi ...

  3. There is no tracking information for the current branch

    There is no tracking information for the current branch. Please specify which branch you want to mer ...

  4. C/C++ 排序&&查找算法(面试)

    一.排序 1.冒泡排序 void BubbleSort(int array[],int n) { ; ; ; ; ;i<n - ;i++) /*外循环控制排序的总趟数*/ { flag = ; ...

  5. 让Dreamweaver支持less

    编辑->首选参数->文件类型/编辑器->在代码视图中打开->添加" .less"后缀

  6. 如何设置启动页 LaunchImage

  7. meta的学习记录

    meta的学习记录 最近再学响应式的布局,就先来总结一下我们的viewport. 什么是viewport呢? 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(v ...

  8. ASIHTTPRequest实现https双向认证请求

    什么是双向认证呢?简而言之,就是服务器端对请求它的客户端要进行身份验证,客户端对自己所请求的服务器也会做身份验证.服务端一旦验证到请求自己的客户端为不可信任的,服务端就拒绝继续通信.客户端如果发现服务 ...

  9. html5头部说明

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...

  10. saltstack之(一)系统环境及本地yum源

    1.服务器环境node1:192.168.3.1node2:192.168.3.2 2.主机名和hosts文件node1: node1.xkops.com --主机名[root@node1 ~]# t ...