zoukankan      html  css  js  c++  java
  • java几个经典的算法题目----------二维矩阵算法

    public class testClockwiseOutput { 
    	
    	public static void main(String[] args) {
    		//1、构建矩阵数据
    		int[][] arr = getMatrix();
    		matrixSort(arr, 0, 3);
    	}
    	
    	
    	/**
    	 * 构建矩阵的二维数组
    	 * @return
    	 */
    	public static int[][] getMatrix(){
    		//1、创建一个容量为100的二维数组
    		int[][] arr = new int[100][100];
    		//2、定义矩阵的边数为4
    		int n = 4;
    		//3、加入矩阵的整数
    		int count = 1;
    		
    		//4、添加矩阵数据
    		for(int i=0;i < n;i++){
    			for(int j=0;j < n;j++){
    				System.out.print(count+" ");
    				arr[i][j] = count++;
    			}
    			System.out.println("");
    		}
    		return arr;
    	}
    	
    	
    	/**
    	 * 对矩阵进行排序
    	 */
    	public static void matrixSort(int[][] arr,int start,int end){
    		
    		if(start >= end || end < 0){
    			return ;
    		}
    		
    		for(int i=start;i<=end;i++){
    			System.out.print(arr[start][i]);
    		}
    		
    		for(int i=start+1;i<=end;i++){
    			System.out.print(arr[i][end]);
    		}
    		
    		for(int i = end-1;i >=start;i--){
    			System.out.print(arr[end][i]);
    		}
    		
    		for(int i=end-1;i>start;i--){
    			System.out.print(arr[i][start]);
    		}
    		
    		matrixSort(arr, start+1, end-1);
    	}
    

      

  • 相关阅读:
    MVC 和 MVVM
    Objective-C对象模型及应用
    面试总汇二
    iOS中常用的四种数据持久化方法简介
    iOS应用程序生命周期
    SDWebImage的总结
    面试知识点总汇
    block 的演练和使用
    Java 类 生成数据库表
    sql中写标量函数生成大写拼音首字母
  • 原文地址:https://www.cnblogs.com/hanxue112253/p/9614529.html
Copyright © 2011-2022 走看看