问题:给定 n 行和 m 列的二维数组矩阵。如图所示,以 ZIG-ZAG 方式打印此矩阵。

  从对称的角度来看,通过反复施加滑行反射可以从简单的图案如线段产生规则的之字形。

  主要思想:算法从(0, 0)位置开始水平向右遍历,当到达(0, 1)时沿着反对角线方向左下遍历(利用一个变量控制左下右上方向),内层循环一直遍历到碰到边缘时row++,方向改为右上,沿着反对角线碰到矩阵上边缘时col++,方向变为左下遍历,知道上半部分(包括反对角线遍历完);遍历完半个矩阵(可能是子方矩阵)后,根据当前 row 索引和 col 索引所在位置来判断延伸方向,若碰到边缘,则对应行列索引数加一,没碰到边缘,则按内层循环的思路延伸,然后相同的思路遍历完矩阵剩下部分。

  算法的时间复杂度为 O(n*m),其中 n, m 为输入矩阵的行数和列数,空间复杂度为 O(1)。

  1 package algorithm;
2
3 public class ZArrangement {
4
5
6
7 /**
8 * Z字型编排打印矩阵
9 *
10 * @param arr 矩阵
11 * @param n 指定子矩阵的行
12 * @param m 指定子矩阵的列
13 */
14 private static void zigzagMatrix(int[][] arr, int n, int m) {
15 int row = 0, col = 0;
16
17 /* 行增长变量,控制和的布尔型变量*/
18 boolean row_inc = false;
19
20 // 打印上半(包括反对角线)Z字形图案的矩阵
21 int mn = Math.min(m, n);
22 for (int len = 1; len <= mn; ++len) {
23 for (int i = 0; i < len; ++i) {
24 System.out.print(arr[row][col] + " ");
25
26 if (i + 1 == len) {
27 break;
28 }
29
30 // 如果row_increment值为true,增加行并减少列, 否则递减行并递增列
31 if (row_inc) {
32 //
33 ++row;
34 --col;
35 } else {
36 //
37 --row;
38 ++col;
39 }
40 }
41
42 if (len == mn) {
43 break;
44 }
45
46 // 根据最后的增量更新行或列的值
47 if (row_inc) {
48 ++row;
49 row_inc = false;
50 } else {
51 ++col;
52 row_inc = true;
53 }
54 }
55
56
57 // 更新row和col变量的索引
58 if (row == 0) {
59 if (col == m-1)
60 ++row;
61 else
62 ++col;
63 row_inc = true;
64 } else {
65 if (row == n-1)
66 ++col;
67 else
68 ++row;
69 row_inc = false;
70 }
71
72 // 打印剩下的Z字型图案矩阵
73 int MAX = Math.max(m, n) - 1;
74 for (int len, diag = MAX; diag > 0; --diag) {
75 if (diag > mn)
76 len = mn;
77 else
78 len = diag;
79
80 for (int i = 0; i < len; ++i) {
81 System.out.print(arr[row][col] + " ");
82
83 if (i + 1 == len)
84 break;
85
86 // 根据最后的增量更新行或列的值
87 if (row_inc) {
88 ++row;
89 --col;
90 } else {
91 ++col;
92 --row;
93 }
94 }
95
96 // 更新row和col变量的索引
97 if (row == 0 || col == m-1) {
98 if (col == m-1)
99 ++row;
100 else
101 ++col;
102 row_inc = true;
103 }
104
105 else if (col == 0 || row == n-1) {
106 if (row == n-1)
107 ++col;
108 else
109 ++row;
110 row_inc = false;
111 }
112 }
113 }
114
115 public static void main(String[] args) {
116 int[][] matrix = {
117 {1, 2, 3},
118 {4, 5, 6},
119 {7, 8, 9}
120 };
121 zigzagMatrix(matrix, 3, 3);
122 }
123 }

算法:Z字型(Zigzag)编排的更多相关文章

  1. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  2. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  3. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  4. [LeetCode] Z字型变换

    题目内容: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:" ...

  5. leetcode 6/300 Z字型变换 py

    目录 题目说明 方法一:利用flag 题目说明 方法一:利用flag 简单来说就是利用flag来表示方向,真的神来之笔. class Solution: def convert(self, s: st ...

  6. 06. Z字型变换

    题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...

  7. leetcode 6 z字型变换

    执行用时 :64 ms, 在所有 Python3 提交中击败了99.74%的用户由题目可知 我们的最终字符串会被摆成 numRows 行,那我们理解为 最终结果是numRows个字符串相加 先建立等于 ...

  8. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

随机推荐

  1. Spring Boot中使用@Async的时候,千万别忘了线程池的配置!

    上一篇我们介绍了如何使用@Async注解来创建异步任务,我可以用这种方法来实现一些并发操作,以加速任务的执行效率.但是,如果只是如前文那样直接简单的创建来使用,可能还是会碰到一些问题.存在有什么问题呢 ...

  2. 【C++基础教程】第二课

    一,上次的课后练习答案 1,输出1+2=3 2,输出2 2.25 2.25 2.25 3,第一空iostream或bits/stdc++.h 第二空main(),main(void)或main(int ...

  3. 基于swoole框架hyperf开发的纯API接口化的后台RBAC管理工具hyperfly@v1.0.0发布

    hyperfly@v1.0.0发布 本文地址http://yangjianyong.cn/?p=323转载无需经过作者本人授权 github地址:https://github.com/vankour/ ...

  4. 创建一个新的解耦的Orchard Core CMS网站

    引言本文将介绍创建一个功能齐全.解耦的CMS网站的过程,该网站允许您编辑博客帖子并呈现它们.解耦是一种开发模型,其中站点的前端和后端(管理)托管在同一个Web应用程序中,但只有后端由CMS驱动.然后, ...

  5. javascript 标签轮播

    html <div id="banner-switch"> <!-- 切换内容 --> <div class="notice-content ...

  6. [转载]CentOS 7 用户怎样安装 LNMP(Nginx+PHP+MySQL)

    关于 Nginx (发音 "engine x")这是一款免费.开源.高效的 HTTP 服务器,Nginx是以稳定著称,丰富的功能,结构简单,低资源消耗.本教程演示如何在CentOS ...

  7. YbtOJ#791-子集最值【三维偏序】

    正题 题目链接:http://www.ybtoj.com.cn/contest/123/problem/1 题目大意 给出\(3\)个长度为\(n\)的排列\(A,B,C\).然后一个下标集合\(S\ ...

  8. Maven----将手动下载的jar包以命令行的方式安装到本地MavenRepository中

    1.情景再现:准备实现SprintBoot的热部署功能,因没有对应jar时,在Eclipse中mvn install 会报错: 报错信息: [INFO] --- spring-boot-maven-p ...

  9. Chrome浏览器启动参数大全(命令行参数)

    前言 在开发Web项目当中,浏览器必不可少,而浏览器的启动参数可以帮我们实现很多功能. 常用参数 常用参数请参考下表. 序号 参数 说明 1 --allow-outdated-plugins 不停用过 ...

  10. vue项目中的element-ui地区级联选择器加详细地址push到对象中不显示的问题

    想要实现级联选择器el-cascader和输入框el-input共同组成的详细地址,添加数据时弹出el-drawer嵌套el-form弹窗,然后在el-form添加数据提交后push到el-table ...