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 ...
随机推荐
- linux下svn的co如何排除目录
某些原因想在svn co的时候排除某些目录,可以绕个圈子,分三步来完成: co外层目录: svn checkout --depth empty $URL [$LOCATION] 完成之后,会有一个只包 ...
- 回调的代理(delegate)实现
1.CoreManage.h #import <Foundation/Foundation.h> @protocol SampleProtocol; //声明核心类的属性和方法 @inte ...
- 软件工程概论---max单元测试
题目:一个单元测试,查找list[]中的最大值 编写一个程序对Largest函数进行测试,列举所有测试用例. 思路:首先确保数组不为空,和数组长度不为0,否则输入错误.根据老师所给的函数写一个主函数, ...
- 关于PushKit的使用总结
1.PushKit的认识 (1)概念 ios8苹果新引入了名为pushkit的框架和一种新的push通知类型,被称作voip push.该push方式旨在提供区别于普通apns push的能力,通过这 ...
- struts.xml 配置详解
struts.xml是我们在开发中利用率最高的文件,也是Struts2中最重要的配置文件. 一下分别介绍一下几个struts.xml中常用到的标签 1.<include> 利用includ ...
- [LeetCode]题解(python):090 Subsets II
题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...
- Swift 遇到的报错信息
第一个,没看懂.一开始还以为是不支持iOS7的缘故. dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /var ...
- JS-007-富文本域操作
在日常 web 编写过程中,富文本域几乎成为了一个网站不可页面元素,同时,其也有着各种各样的实现方式,网络上也存在着各种各样的集成插件可供引用.此文以 js 获取.修改 163 邮箱写邮件时的邮件内容 ...
- 【转】Android Paint之 setXfermode PorterDuffXfermode 讲解
[置顶] Android Paint之 setXfermode PorterDuffXfermode 讲解 分类: android动效篇2015-04-07 17:23 978人阅读 评论(8) 收藏 ...
- LeetCode Basic Calculator
原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...