LintCode #46. Matrix Zigzag Traversal (Easy)

class Solution {
public:
vector<int> printZMatrix(vector<vector<int> > &matrix) {
vector<int> v;
if (matrix.size() == 0) return v;
int m = matrix.size(), n = matrix[0].size(), cnt = m * n;
int i = 0, j = 0;
int d[][2] = {{ -1, 1 }, { 1, -1 }};
int dir = 0;
while (v.size() < cnt) {
while (i >= 0 && i < m && j >= 0 && j < n) {
v.push_back(matrix[i][j]);
i += d[dir][0];
j += d[dir][1];
}
i -= d[dir][0];
j -= d[dir][1];
if (dir == 0) {
if (j + 1 < n) ++j;
else ++i;
} else {
if (i + 1 < m) ++i;
else ++j;
}
dir = (dir + 1) % 2;
}
return v;
}
};

思路:

  • 斜着走的方向只有"右上"(dir=0)和"左下"(dir=1). 按"右上", "左下"的顺序交替走.
  • 当走到边界的时候, 要么"向右走一步", 要么"向下走一步".
    • 如果正在向"上"走, 优先"向走一步", 若不能则"向走一步".
    • 如果正在向"下"走, 优先"向走一步", 若不能则"向走一步".

时间复杂度: O(m*n)

空间复杂度: O(1)


参照这篇博文的思路.

class Solution {
public:
/**
* @param matrix: a matrix of integers
* @return: a vector of integers
*/
vector<int> printZMatrix(vector<vector<int> > &matrix) {
vector<int> v;
if (matrix.size() == 0) return v;
int m = matrix.size(), n = matrix[0].size();
int sum = 0, x = 0, dx = -1;
while (sum < m + n - 1) {
while (x >= 0 && x < m && sum - x >= 0 && sum - x < n) {
v.push_back(matrix[x][sum - x]);
x += dx;
}
x -= dx;
sum++;
if ((dx == -1 && sum - x >= n)
|| (dx == 1 && x < m - 1)) {
++x;
}
dx = -dx;
}
return v;
}
};

思路:

  • 观察下标规律.
(0, 0)
(0, 1), (1, 0)
(2, 0), (1, 1), (0, 2)
(0, 3), (1, 2), (2, 1)
(2, 2), (1, 3)
(2, 3)

可以看到各行xy坐标的和是常数, 且该和逐行递增.

  • 偶数行x递减, 奇数行x递增.
  • 原解法有个缺陷是"矩阵越细长, 空循环就越多". 我的解法对这点进行了优化.

    时间复杂度: O(m*n)

    空间复杂度: O(1)

[OJ] 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. 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 ...

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

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

  4. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

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

  5. [LintCode]——目录

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

  6. Java Algorithm Problems

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

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

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

  8. PAT甲级1127. ZigZagging on a Tree

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

  9. LeetCode OJ: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. YII中引用自定义类

    如果通过actions方法引用其他自定义类时 <?php class LoginController extends Controller { public function actionInd ...

  2. linux的终端,网络虚拟终端,伪终端(转)

    转自http://www.xuebuyuan.com/877887.html 2013年09月07日 ⁄ 综合 ⁄ 共 4047字 ⁄ 字号 小 中 大 ⁄ 评论关闭 Linux上许多网络服务应用,如 ...

  3. (转)Asp.NetURL重写的一种方法

    说到不用设置iis,主要是为了实现在虚拟主机或是拿不到iis操作限的时候,不能添加isap又想实现类似于静态化的程序实现方式,先声明,这里最终要实现的效果是,最终可以用 12345.html 替换 s ...

  4. MVC LINQ中用封装的TSQL通用更新方法

    把TSQL拿出来,做了一个封装,适用的所有表,更新有两种,普通更新和记数更新 看代码:这两个方法是写在DAL里的数据操作基类里的,只有它的子类可以用它,所以用protected做为限制 /// < ...

  5. Git for Windows

    本篇文章由:http://www.sollyu.com/git-for-windows/ 说明 Git是用于Linux内核开发的版本控制工具.与CVS.Subversion一类的集中式版本控制工具不同 ...

  6. 九度0J 1374 所有员工年龄排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1374 题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你 ...

  7. (转) IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

  8. Page类成员

    1. Request,Response,Server属性:对contex.Request,context.Response,context.Server的简化调用2. AppRelativeVirtu ...

  9. ECMA5.1中关于encodeURI,decodeURI 和encodeComponentURI,decodeComponentURI的区别

    The encodeURI and decodeURI functions are intended to work with complete URIs; theyassume that any r ...

  10. Linux网络应用编程之集线器(Packet Tracer仿真)

    Packet Tracer入门 一,集线器概况 对接收到的信号进行再生整形放大,以扩大网络的传输距离,同时把所有节点集中在以它为中心的节点上. 工作于OSI(开放式系统互联参考模型)的最底层(物理层) ...