算法篇6:数组与矩阵的操作(正方矩阵旋转,之字形打印)

旋转正方形矩阵

【题目】

给定一个整型正方形矩阵matrix,请把该矩阵调整成 顺时针旋转90度的样子。 【要求】 额外空间复杂度为O(1)。

解题思路:

从宏观角度来看,我们只需将最外圈转对,里面一样转即可,外围圈只需找到对应点,然后交换位置,那我们只需确定这四个对应点即可。

 1  2  3  4  

5  6  7  8 

9  10  11  12

13  14  15  16

则对应1,4,16,13一组,··········                                                                                                               

代码实现:

 

 1 package com.xqc.sort;
 2 
 3 public class RotateMatrix {
 4 
 5     public static void rotate(int[][] matrix) {
 6         int tR = 0;
 7         int tC = 0;
 8         int dR = matrix.length - 1;
 9         int dC = matrix[0].length - 1;
10         while (tR < dR) {
11             rotateEdge(matrix, tR++, tC++, dR--, dC--);
12         }
13     }
14 
15     public static void rotateEdge(int[][] m, int tR, int tC, int dR, int dC) {
16         int times = dC - tC; 
17         int tmp = 0;
18         for (int i = 0; i != times; i++) {
19             
20             //寻找每圈对应的点
21             tmp = m[tR][tC + i];
22             m[tR][tC + i] = m[dR - i][tC];
23             m[dR - i][tC] = m[dR][dC - i];
24             m[dR][dC - i] = m[tR + i][dC];
25             m[tR + i][dC] = tmp;
26         }
27     }
28 
29     public static void printMatrix(int[][] matrix) {
30         for (int i = 0; i != matrix.length; i++) {
31             for (int j = 0; j != matrix[0].length; j++) {
32                 System.out.print(matrix[i][j] + " ");
33             }
34             System.out.println();
35         }
36     }
37 
38     public static void main(String[] args) {
39         int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
40                 { 13, 14, 15, 16 } };
41         printMatrix(matrix);
42         rotate(matrix);
43         System.out.println("=========");
44         printMatrix(matrix);
45 
46     }
47 
48 }

 

之字型打印:

【题目】

给定一个矩阵matrix,按照“之”字形的方式打印这 个矩阵,例如: 1 2 3 4 5 6 7 8 9 10 11 12 “之”字形打印的结果为:1,2,5,9,6,3,4,7,10,11, 8,12 【要求】 额外空间复杂度为O(1)。

解题思路:

我们只需要定义两个指针A 和指针B,让A先往右走,走到头再往下,B先往下走,走到头再往右,这样A和B所连成的线即位打印所需对角线,我们只需判断应该从上往下,还是重下往上打印即可。

代码实现:

 1 package com.xqc.sort;
 2 
 3 public class ZigZagPrintMatrix {
 4 
 5     public static void printMatrixZigZag(int[][] matrix) {
 6         //指针A的行数与列数
 7         int aR = 0;
 8         int aC = 0;
 9         //指针B的行数与列数
10         int bR = 0;
11         int bC = 0;
12         //行数与列数的最大值
13         int endR = matrix.length - 1;
14         int endC = matrix[0].length - 1;
15         
16         boolean fromUp = false;
17         while (aR != endR + 1) {
18             
19             printLevel(matrix, aR, aC, bR, bC, fromUp);
20             //判断指针A与指针B怎么走
21             aR = aC == endC ? aR + 1 : aR;
22             aC = aC == endC ? aC : aC + 1;
23             bC = bR == endR ? bC + 1 : bC;
24             bR = bR == endR ? bR : bR + 1;
25             fromUp = !fromUp;
26         }
27         
28         System.out.println();
29     }
30 
31     public static void printLevel(int[][] m, int tR, int tC, int dR, int dC,
32             boolean f) {
33         if (f) {
34             while (tR != dR + 1) {
35                 System.out.print(m[tR++][tC--] + " ");
36             }
37         } else {
38             while (dR != tR - 1) {
39                 System.out.print(m[dR--][dC++] + " ");
40             }
41         }
42     }
43 
44     public static void main(String[] args) {
45         int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
46         printMatrixZigZag(matrix);
47 
48     }
49 
50 }

 

posted @ 2018-08-30 11:17  阿苍老师  阅读(413)  评论(0编辑  收藏  举报