Matrix Zigzag Traversal(LintCode)
Matrix Zigzag Traversal
Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in ZigZag-order.
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]
public class Solution {
/**
* @param matrix: a matrix of integers
* @return: an array of integers
*/
public int[] printZMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int l = matrix.length*matrix[0].length;
int[] result = new int[l];
int d = -1;
int x = 0;
int y = 0;
for(int i=0;i<l;i++) {
result[i] = matrix[x][y];
if(x+d < 0 && y < n-1 || x+d >= m && y >= 0) {
d = -d;
y++;
}else {
if(y-d < 0 && x < m-1 || y-d >= n && x >= 0) {
d = -d;
x++;
}else {
x = x + d;
y = y - d;
}
}
}
return result;
}
}
Matrix Zigzag Traversal(LintCode)的更多相关文章
- [OJ] Matrix Zigzag Traversal
LintCode #46. Matrix Zigzag Traversal (Easy) class Solution { public: vector<int> printZMatrix ...
- 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 ...
- 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道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...
- 2 - Binary Search & LogN Algorithm
254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...
随机推荐
- 统计Visual Studio项目的代码行数
原文转自 https://blog.csdn.net/tyc129/article/details/74279806 使用Visual Studio 自带的在文件中查找功能中的正则表达式实现代码统计功 ...
- Python3 使用 urllib 编写爬虫
什么是爬虫 爬虫,也叫蜘蛛(Spider),如果把互联网比喻成一个蜘蛛网,Spider就是一只在网上爬来爬去的蜘蛛.网络爬虫就是根据网页的地址来寻找网页的,也就是URL.举一个简单的例子,我们在浏览器 ...
- Linking code for an enhanced application binary interface (ABI) with decode time instruction optimization
A code sequence made up multiple instructions and specifying an offset from a base address is identi ...
- 25个Linux相关的网站【转】
转自:http://www.cnblogs.com/Lindaman/p/4552805.html 下面是25个最具有影响力,也是最重要的Linux网站,这些网站提供了Linux的分发包,软件,文件, ...
- HTML综合实例【月福首页】
1. 首页的布局结构 2. 排版的准备工作 网站的素材:与当前网页放在一起. 创建一个html文件,来进行排版. 网页的背景色 确定主页的宽度:778px 3. HTML注释 <!-- 注释内容 ...
- java===java基础学习(7)---用户自定义类
package testbotoo; import java.util.*; public class EmployeeTest { public static void main(String[] ...
- 【Educational Codeforces Round 19】
这场edu蛮简单的…… 连道数据结构题都没有…… A.随便质因数分解凑一下即可. #include<bits/stdc++.h> #define N 100005 using namesp ...
- bzoj 1798 维护序列seq
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 题解: 高级一点的线段树,加上了区间乘法运算,则需要增加一个数组mulv记录乘的因数 ...
- codevs 1038 一元三次方程求解 NOIP2001提高组
题目链接:http://codevs.cn/problem/1038/ 题解: 嗯,exm?才知道二分隶属搜索专题…… 对-100到100枚举,按照题目中的提示,当当fi*fi+1<0时,二分深 ...
- C 实现有追求的线程池 探究
引言 线程池很普通的老话题,讨论的很多.深入的不多,也就那些基础库中才能见到这种精妙完备的技巧.而本文随大流 想深入简述一种高效控制性强的一种线程池实现. 先引入一个概念, 惊群. 简单举个例子. 春 ...