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字形层次 ...
随机推荐
- 51Nod 1082 | 模拟
Input示例 5 4 5 6 7 8 Output示例 30 55 91 91 155 模拟 #include "bits/stdc++.h" using namespace s ...
- Data Mining的十种分析方法——摘自《市场研究网络版》谢邦昌教授
Data Mining的十种分析方法: 记忆基础推理法(Memory-Based Reasoning:MBR) 记忆基础推理法最主要的概念是用已知的案例(case)来预测未来案例的一些属 ...
- iOS tag的使用
一.添加标记 (标记不能为0) UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(,,,)]; backBtn.backgr ...
- 省队集训Day1 总统选举
[题目大意] 一个$n$个数的序列,$m$次操作,每次选择一段区间$[l, r]$,求出$[l, r]$中出现超过一半的数. 如果没有超过一半的数,那么就把答案钦定为$s$,每次会有$k$个数进行改变 ...
- LeetCode 20 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- DSP学习教程基于28335(一)
首先说明:开发环境Manjaro linux,内核5.0,滚动升级版本,随时都是最新,CCS也是最新的CCv 8 #include "DSP2833x_Device.h" // 这 ...
- Linux 入门记录:二、Linux 文件系统基本结构
一.树状目录结构 Linux 文件系统是一个倒置的单根树状结构.文件系统的根为"/":文件名严格区分大小写:路径使用"/"分割(Windows 中使用" ...
- 【学习笔记】动态树Link-Cut-Tree
这是两个月前写的,看能不能搬运过来…… 动态树是一类维护森林连通性的问题(已纠正,感谢ZQC巨佬),目前最(wo)常(zhi)见(hui)的动态树就是LCT(Link-Cut-Tree),然而LCT似 ...
- 设计模式之笔记--单例模式(Singleton)
单例模式(Singleton) 定义 单例模式(Singleton),保证一个类仅有一个实例,并提供一个访问它的全局访问点. 类图 描述 类Singleton的构造函数的修饰符为private,防止用 ...
- android intent 传数据
1. 基本数据类型 Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 ...