• 数组和矩阵问题


    给定一个整型矩阵matrix,用转圈方式打印它

     要求额外空间复杂度:O(1)

    这里介绍一种 矩阵处理方式,   矩阵分圈处理!!!!!!

    思路:  

      在矩阵中庸左上角的坐标(tR, tC)和右下角(dR, dC)就可以表示一个子矩阵。

      比如 当(tR, tC)=(0,0)  (dR,dC)=(3,3)时, 表示的子矩阵就是整个矩阵

         然后移动 转圈打印就ok了  限制条件是 左上方 跟 右下方的点 相遇

    package TT;
    
    public class Test14 {
    
    	public static void spiralOrderPrint(int[][] matrix){
    		
    		int tR=0;
    		int tC=0;
    		int dR = matrix.length-1;
    		int dC = matrix[0].length-1;
    		
    		while(tR<=dR && tC<=dC){
    			printEdge(matrix, tR++, tC++, dR--, dC--);
    			
    			
    		}
    
    	}
    	
    	public static void printEdge(int[][] m, int tR, int tC, int dR, int dC){
    		
    		     if(tR==dR){  //子矩阵只有一行时
    		    	 
    		    	 for(int i = tC; i<=dC; i++){
    		    		 System.out.println(m[tR][i]+"");
    		    	 }
    		    	 
                }else if(tC==dC){
                	for (int i = tR; i <dR; i++) {
                		
    					System.out.println(m[i][tC]+"");
    				}
                }else {
    				int curC = tC;
    				int curR = tR;
    				while(curC !=dC){
    					System.out.println(m[tR][curC]+" ");
    					curC++;
    				}
    				while(curR != dR){
    					System.out.println(m[curR][dC]+" ");
    					curR++;
    				}
    				while(curC!=tC){
    					System.out.println(m[dR][curC]+" ");
    				    curC--;
    				}
    				while(curR !=tC){ 
    					System.out.println(m[curR][tC]+" ");
    					curR--;
    				}
    				
    				
    			}
    		     
    	
    	}
    	
    	
    	public static void main(String[] args){
    		
    		int[][] m = new int[3][3];
    		m[0][0]=1; m[0][1]=2;m[0][2]=3;
    		m[1][0]=4; m[1][1]=5;m[1][2]=6;
    		m[2][0]=7;m[2][1]=8;m[2][2]=9;
    			
    		spiralOrderPrint(m);
    		
    		
    		
    	}
    	
    }
    

      测试结果:

    其实本题目不难,思路很清晰,重点在于代码实现过程中的代码简洁和技巧。

    while 变量的控制问题

    所以额外设计了一个函数,弹栈不影响原有的while变量值

    public class Test4 {
    
        public static void roundPrint(int[][] arr) {
            int row = 0;
            int col = 0;
    
            int rowEnd = arr.length-1;
            int colEnd = arr[0].length-1;
    
            while (row !=rowEnd && col !=colEnd) {
                roundPriting(arr, row, col, rowEnd, colEnd);
                row++;
                col++;
                rowEnd--;
                colEnd--;
            }
            //一行或者一列时候的问题 同处一条线上的问题
            if (col == colEnd) { //同一列
                while (row<=rowEnd) {
                    System.out.println(arr[row][col]);
                    row++;
                }
            }else if (row==rowEnd) { //同一行
                while (col<=colEnd) {
                    System.out.println(arr[row][col]);
                    col++;
                }
            }
        }
        public static void roundPriting(int[][] arr, int row, int col, int rowEnd, int colEnd) {
            while (row<rowEnd) {
                System.out.print(arr[row][col]+",");
                row++;
            }
            while (col<colEnd) {
                System.out.print(arr[row][col]+",");
                col++;
            }
            while (row > 0) {
                System.out.print(arr[row][col]+",");
                row--;
            }
            while (col > 0) {
                System.out.print(arr[row][col]+",");
                col--;
            }
    
        }
    
        public static void main(String[] args) {
            int[][] arr = new int[3][4];
            arr[0][0] = 1;
            arr[1][0] = 2;
            arr[2][0] = 3;
            arr[0][1] = 4;
            arr[1][1] = 5;
            arr[2][1] = 6;
            arr[0][2] = 7;
            arr[1][2] = 8;
            arr[2][2] = 9;
            arr[0][3] = 10;
            arr[1][3] = 11;
            arr[2][3] = 12;
            roundPrint(arr);
        }
    }
  • 相关阅读:
    [NOI2007]生成树计数
    [NOI2009]变换序列
    BZOJ3261 最大异或和
    [SHOI2011]双倍回文
    [TopCoder14647]HiddenRabbits
    [HDU5709]Claris Loves Painting
    [BZOJ4559][JLOI2016]成绩比较
    [CF995F]Cowmpany Cowmpensation
    [TopCoder11557]MatrixPower
    [UOJ198][CTSC2016]时空旅行
  • 原文地址:https://www.cnblogs.com/toov5/p/7420001.html
Copyright © 2020-2023  润新知