Lintcode: Matrix Zigzag Traversal
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的更多相关文章
- Matrix Zigzag Traversal(LintCode)
Matrix Zigzag Traversal Given a matrix of m x n elements (m rows, ncolumns), return all elements of ...
- [OJ] Matrix Zigzag Traversal
LintCode #46. Matrix Zigzag Traversal (Easy) class Solution { public: vector<int> printZMatrix ...
- lintcode:Matrix Zigzag Traversal 矩阵的之字型遍历
题目: 矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- 别再埋头刷LeetCode之:北美算法面试的题目分类,按类型和规律刷题,事半功倍
算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...
- [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 ...
随机推荐
- 动态加载js,css
今天项目中需要用到动态加载 CSS 文件,经过一番折腾终于搞定,回家整理了一下,顺便融合了动态加载 JS 的功能写成了一个对象,先上代码: var dynamicLoading = { css: fu ...
- Sphinx+MySQL5.1x+SphinxSE+mmseg
一.不停止mysql的情况下安装SphinxSE 1.确定mysql版本,下载对应源码包 此处下载5.1.69的mysql源码包 #wget ftp://ftp.ntu.edu.tw/pub/MySQ ...
- 微信自定义菜单说php json_encode不转义中文汉字的方法
http://blog.csdn.net/qmhball/article/details/45690017 最近在开发微信自定义菜单. 接口比较简单,就是按微信要求的格式post一段json数据过去就 ...
- mysqli常用命令
<?php //创建连接 $mysqli=new mysqli("localhost","root","","volunte ...
- 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的
while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...
- linux卸载php
卸载: rpm -qa | grep php 所以正确的卸载顺序是:# rpm -e php-mysql-5.1.6-27.el5_5.3 --allmatches #同名全部卸载# rpm -e ...
- hadoop与云技术、云计算混肴澄清
本文引用自:http://www.aboutyun.com/blog-61-248.html 一.初学者问题: 请教个问题在实际的生成环境里面,数据源产生的地方部署Hadoop,还是需要程序把数据给迁 ...
- 利用JS脚本通过getAttribute()和setAttribute()等对CSS样式进行操作
HTML中引入CSS样式的方式有三种: 1.最常用的,引入样式表,在样式表中编写样式,引入方式如下:<link href="css/style.css" rel=" ...
- android Listview item 中有button,item就不响应触摸事件
为button设置 beanButton.getButton().setFocusable(false); beanButton.getButton().setFocusableInTouchMode ...
- java构造函数,java的静态块理解
今天我遇到这样的代码块,理解甚久,现在理解了,举一些例题给你看看 先创建一个One类: package accp.com;/** * 其中一个类 * @author xuxiaohua * */pub ...