矩阵的应用

Z形打印二维数组

package 矩阵;

/**
 * @author: DreamCode
 * @file: Z形打印二维数组.java
 * @time: 2022年3月9日-下午4:28:21
 * @思路: 模拟遍历
 */
public class Z形打印二维数组 {

	/**
	 * z字形打印矩阵
	 */
	public static void main(String[] args) {
		 int[][] matrix = {
			        {1, 2, 3, 4},
			        {5, 6, 7, 8},
			        {9, 10, 11, 12},
			        {13, 14, 15, 16},
			    };
		 printZMatrix(matrix);

	}

	private static void printZMatrix(int[][] matrix) {
		// TODO Z形打印二维数组
		int r=0,m=matrix.length-1;
		int c=0,n=matrix[0].length-1;
		boolean l2r=true; //是否从左到右
		while(r<=m&&c<=n) {
			if (l2r) {//从左到右打印
				if(r==0&&c<n) {//当前位于第一行,向右走
					System.out.print(matrix[r][c]+" ");
					l2r=!l2r;
					c++;
					continue;
				}else if (c==n&&r<m) { //当前位于最后一列,向下走
					System.out.print(matrix[r][c]+" ");
					l2r=!l2r;
					r++;
					continue;
				}else {
					System.out.print(matrix[r][c]+" ");
					r--;
					c++;
				}
				}else { //从右到左打印
					if(c==0&&r<m) {//当前位于第一列,向下打印
						System.out.print(matrix[r][c]+" ");
						l2r=!l2r;
						r++;
						continue;
					}else if(r==m&&c<n) {//当前位于最后一行,向右打印
						System.out.print(matrix[r][c]+" ");
						l2r=!l2r;
						c++;
						continue;
					}else {
						System.out.print(matrix[r][c]+" ");
						r++;
						c--;
					}		
			}
		}
		
	}

}

边界为1的最大方阵

给定一个N×N的矩阵matrix,在这个矩阵中,只有0和1两种值,返回边框全是1的最大正方形的边长长度。
例如:
{0, 1, 1, 1, 1},
{0, 1, 0, 0, 1},
{0, 1, 0, 0, 1},
{0, 1, 1, 1, 1},
{0, 1, 0, 1, 1} 
其中,边框全是1的最大正方形的大小是4*4,返回4

package 矩阵;

/**
 * @author: DreamCode
 * @file: 边界为1的最大方阵.java
 * @time: 2022年3月9日-下午5:06:55
 * @思路: 模拟
 */
public class 边界为1的最大方阵 {

	public static void main(String[] args) {
//		int[][] A = {
//			        {0, 1, 1, 1, 1},
//			        {0, 1, 0, 1, 0},
//			        {0, 1, 1, 1, 1},
//			        {0, 1, 1, 1, 1},
//			        {0, 1, 0, 1, 1}
//			    };
		int[][] A = new int[][]{
	        {1, 1, 1, 1},
	        {1, 0, 1, 1},
	        {1, 1, 1, 1},
	        {1, 0, 1, 1},
	    };
		int res = getMaxMatrix(A);
		System.out.println(res);

	}

	private static int getMaxMatrix(int[][] a) {
		int n = a.length;
		int len = n;
		while(n>0) {
			for(int i=0;i<=len-n;i++) {
				l3:
				for(int j=0;j<=len-n;j++) {
					//水平检查
					int r=i,c=j;
					while(c<j+n) {
						if(a[r][c++]==0)continue l3;
					}
					c--;
					//右边界检查
					while(r<i+n) {
						if(a[r++][c]==0)continue l3;
					}
					r--;
					//下边界检查
					while(c>=j) {
						if(a[r][c--]==0)continue l3;
					}
					c++;
					//左边界检查
					while(r>=i) {
						if(a[r--][c]==0)continue l3;
					}
					//走到了最后
					return n;
				}
			}
			n--;
		}
		return 0;
	}

}

边界为1的最大方阵_优化

给定一个N×N的矩阵matrix,在这个矩阵中,只有0和1两种值,返回边框全是1的最大正方形的边长长度。
例如:
{0, 1, 1, 1, 1},
{0, 1, 0, 0, 1},
{0, 1, 0, 0, 1},
{0, 1, 1, 1, 1},
{0, 1, 0, 1, 1} 
其中,边框全是1的最大正方形的大小是4*4,返回4

package 矩阵;

import java.util.Iterator;

/**
 * @author: DreamCode
 * @file: 边界为1的最大方阵_优化.java
 * @time: 2022年3月9日-下午7:23:35
 * @思路: 模拟+预处理
 */
public class 边界为1的最大方阵_优化 {

	static int[][][] rec;
	public static void main(String[] args) {
		 int[][] A = {
			        {0, 1, 1, 1, 1},
			        {0, 1, 0, 1, 0},
			        {0, 1, 1, 1, 1},
			        {0, 1, 1, 1, 1},
			        {0, 1, 0, 1, 1}
			    };
//		 int[][] A = new int[][]{
//			        {1, 1, 1, 1},
//			        {1, 0, 0, 1},
//			        {1, 1, 1, 1},
//			        {1, 1, 1, 1},
//			    };
		 genertateHelpRec(A);
		 printRec();
		int res = getMaxMatrix(A);
		System.out.println(res);
	}
	private static int getMaxMatrix(int[][] a) {
		int len = a.length;
		int n=len;
		while(n>0) {
			for(int i=0;i<=len-n;i++) {
				for(int j=0;j<=len-n;j++) {
					if(check(i,j,n)) {
						return n;
					}
				}
			}
			n--;
		}
		return 0;
		
	}
	private static boolean check(int i, int j, int n) {
		if(rec[i][j][0]>=n&&rec[i][j][1]>=n&&rec[i+n-1][j][0]>=n&&rec[i][j+n-1][1]>=n) {
			return true;
		}
		return false;
	}
	private static void printRec() {
		for(int i=0;i<rec.length;i++) {
			for(int j=0;j<rec[i].length;j++) {
				System.out.print(rec[i][j][0]+","+ rec[i][j][1]+'\t');
			}
			System.out.println();
		}
		
	}
	private static void genertateHelpRec(int[][] a) {
		// TODO 数据预处理
		int n = a.length;
		rec = new int[n][n][2];
		//处理最后一行n-1
		int row = n-1;
		for(int i=n-1;i>=0;i--) {
			if(a[row][i]==1) {//当前的位置为1
				if(i==n-1) {//为最后一个元素
					rec[row][i][0]=1;
				}else {
					rec[row][i][0]=rec[row][i+1][0]+1;
				}
				rec[row][i][1]=1;
			}
		}
		//处理0-n-2行
		for(int i=n-2;i>=0;i--) {
			for(int j=n-1;j>=0;j--) {
				if(a[i][j]==1) {//当前值为1
					if(j==n-1) {//当前为最后一列
						rec[i][j][0]=1;
					}else {
						rec[i][j][0]=rec[i][j+1][0]+1;
					}
					rec[i][j][1]=rec[i+1][j][1]+1;
				}
			}
		}
		
	}

}

将0所在的行列清零

package 矩阵;

/**
 * @author: DreamCode
 * @file: 将0所在的行列清零.java
 * @time: 2022年3月9日-下午4:06:28
 * @思路:  遍历
 */
public class0所在的行列清零 {

	public static void main(String[] args) {
		int[][] matrix = { 
				{ 1, 2, 3, 4, 100 }, 
				{ 5, 6, 7, 0, 101 }, 
				{ 9, 0, 11, 12, 102 },
				{ 13, 14, 15, 16, 103 },
				{ 104, 105, 106, 107, 103 }, };
		solve(matrix);
		printMatrix(matrix);
	}

	private static void printMatrix(int[][] matrix) {
		// TODO 打印二维数组
		for(int arr[]: matrix) {
			for(int num:arr) {
				System.out.print(num+" ");
			}
			System.out.println();
		}
		
	}

	private static void solve(int[][] matrix) {
		int M = matrix.length;
		int N = matrix[0].length;
		int[] RecordRow = new int[M];
		int[] RecordCol = new int[N];
		for(int i=0;i<M;i++) {
			for(int j=0;j<N;j++) {
				if(matrix[i][j]==0) {
					RecordCol[j]=1;
					RecordRow[i]=1;
				}
			}
		}
		for(int i=0;i<M;i++) {
			for(int j=0;j<N;j++) {
				if(RecordRow[i]==1||RecordCol[j]==1) {
					matrix[i][j]=0;
				}
			}
		}
	}

}

顺序打印二维数组

顺时针打印二维数组
输入:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

package 矩阵;

/**
 * @author: DreamCode
 * @file: 顺序打印二维数组.java
 * @time: 2022年3月9日-下午3:47:40
 * @思路: 模拟过程
 */
public class 顺序打印二维数组 {

	public static void main(String[] args) {
		int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
		printMatrix(arr);

	}

	private static void printMatrix(int[][] arr) {
		int LeftUpRow=0,LeftUpCol=0,RightDownRow=arr.length-1,RightDownCol=arr[0].length-1;
		while(LeftUpCol<=RightDownCol&&LeftUpRow<=RightDownRow) {
			int r=LeftUpCol,c=LeftUpCol;
			//上边
			while(c<=RightDownCol) {
				System.out.print(arr[r][c++]+" ");
			}
			//复位
			c=RightDownCol;
			r++;
			while(r<=RightDownRow) {
				System.out.print(arr[r++][c]+" ");
			}
			r = RightDownRow;
			c--;
			//下边
			while(c>=LeftUpCol) {
				System.out.print(arr[r][c--]+" ");
			}
			c=LeftUpCol;
			r--;
			//左边
			while(r>LeftUpRow) {
				System.out.print(arr[r--][c]+" ");
			}
			LeftUpCol+=1;
			LeftUpRow+=1;
			RightDownCol-=1;
			RightDownRow-=1;
		}
		
		
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦码城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值