lintcode-185-矩阵的之字型遍历
185-矩阵的之字型遍历
给你一个包含 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]标签
LintCode 版权所有 矩阵
思路
参考http://blog.csdn.net/wutingyehe/article/details/46629087
code
class Solution {
public:
/**
* @param matrix: a matrix of integers
* @return: a vector of integers
*/
vector<int> printZMatrix(vector<vector<int> > &matrix) {
// write your code here
int sizeRow = matrix.size();
if (sizeRow <= 0) {
return vector<int>();
}
int sizeCol = matrix[0].size();
if (sizeCol <= 0) {
return vector<int>();
}
vector<int> result;
int count = sizeRow * sizeCol, curRow = 0, curCol = 0;
result.push_back(matrix[0][0]);
for (int i = 1; i < count; ) {
//斜上走到顶
while (i < count && curRow - 1 >= 0 && curCol + 1 < sizeCol) {
result.push_back(matrix[--curRow][++curCol]);
i++;
}
//横右走一步,不可横右走时竖下走一步
if (i < count && curCol + 1 < sizeCol) {
result.push_back(matrix[curRow][++curCol]);
i++;
}
else if (i < count && curRow + 1 < sizeRow) {
result.push_back(matrix[++curRow][curCol]);
i++;
}
//斜下走到底
while (i < count && curRow + 1 < sizeRow && curCol - 1 >= 0) {
result.push_back(matrix[++curRow][--curCol]);
i++;
}
//竖下走一步,不可竖下走时横右走一步
if (i < count && curRow + 1 < sizeRow) {
result.push_back(matrix[++curRow][curCol]);
i++;
}
else if (i < count && curCol + 1 < sizeCol) {
result.push_back(matrix[curRow][++curCol]);
i++;
}
}
return result;
}
};
lintcode-185-矩阵的之字型遍历的更多相关文章
- lintcode:Matrix Zigzag Traversal 矩阵的之字型遍历
题目: 矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9 ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- 算法:Z字型(Zigzag)编排
问题:给定 n 行和 m 列的二维数组矩阵.如图所示,以 ZIG-ZAG 方式打印此矩阵. 从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形. 主要思想:算法从(0, 0) ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [函數] Firemonkey Android 取得系统参数设定的字型大小
Android 系统参数设定内,可以设定字型大小: 可以透过下面代码来取得字型大小比例: function FontScale: Single; var Resources: JResources; ...
- Delphi 取得 iOS 辅助使用里的字型大小
说明:在 iOS 里有一个人性化的辅助设定,可以将字体放大,但这个设定对 Delphi 是不起作用的,还好 Delphi 提供了这个 iOS API 可以取得. 开发环境:Delphi 10 Seat ...
- Firemonkey TComboBox 下拉菜单字型修改方法 (D10)
在 FMX 下的 TComboBox 下拉菜单字型修改有二种方法: uses FMX.Pickers; 使用 Style,需先设定好 Style 后,再指定预设项的 Style,方法如下: proce ...
- XE7 iOS 取得系统字型名称
系统字型名称在每个平台的取得方式不尽相同,以下示范如何在 iOS 取得系统内所有字型的名称: uses // 加入下面二个 uses 文件 iOSapi.UIKit, iOSapi.Foundatio ...
随机推荐
- rem和em的用法
1.rem转化为向素值的方法 rem单位转化为像素大小取决于根元素的字体大小,即HTML元素的字体大小,根元素字体大小乘以rem. 例:根元素的字体大小 16px,10rem 将等同于 160px,即 ...
- 【JavaWeb】从零实现用户登录
1.数据库预备 1.1 SQL 创建数据库 create database db; 创建表 create table userInfo( id int primary key , name ), pa ...
- 谈谈php对象的依赖
通过构造函数的方法 <?php //定义一个类,后面的类依赖这个类里面的方法 class play { public function playing() { echo "I can ...
- ArrayList的源码分析(基于jdk1.8)
1.初始化 transient Object[] elementData; //实际存储元素的数组 private static final Object[] DEFAULTCAPACITY_EMPT ...
- hadoop jobhistory访问界面长时间打不开
1.浏览器无法直接通过url访问 可能原因 :主机名未配置,因此无法识别,在 c:\windows\system32\drivers\etc 目录添加主机名和对应ip hostname1[主机名 ] ...
- php7+apache2.4+mysql 环境配置(window环境)
最近,小主从事PHP开发.特将最近如何搭建php7的过程记录在此!希望有需要,可以借鉴!( 电脑必须win7 sp1以上, .netframework4 ) Windows7安装php7,Win7+p ...
- 北京Uber优步司机奖励政策(3月8日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- OpenCV 3.2 FlannBasedMatcher
#include <iostream> #include <string> #include <boost/timer.hpp> #include "op ...
- Python3 linux安装
./configure --prefix=/usr/local/python3 --with-ssl --enable-optimizations make && make insta ...
- jQuery wordexport导出 word
同事给我说了简单的导出word的插件,亲测了下,做个随笔. 这个导出插件是jQuery自带的的插件,通过调用wordexport.js来实现导出功能. 1.引入的js <script type= ...