JAVA编程题-求矩阵螺旋值

螺旋类

java 复制代码
package entity;
/**
 * 打印数组螺旋值类
 */
public class Spiral {
//	数组行
	private int row;
//	数组列
	private int col;
//	行列数
	private int size;
//	当前行索引
	private int rowIndex;
//	当前列索引
	private int colIndex;
//	行开始索引
	private int rowStart;
//	行结束索引
	private int rowEnd;
//	列开始索引
	private int colStart;
//	列结束索引
	private int colEnd;
//	数组
	private int[][] spiralArray;
//	保存数组
	private int[][] saveArray;
//	构造方法
	public Spiral(int row,int col,int[][] spiralArray) {
		this.row = row;
		this.col = col;
		this.rowStart = 0;
		this.rowEnd = row - 1;
		this.colStart = 0;
		this.colEnd = col - 1;
		this.size = row*col;
		this.spiralArray = spiralArray;
		this.saveArray = new int[row][col];
		for(int[] r : this.saveArray) {
			for(int i=0;i<r.length;i++) {
				r[i] = Integer.MIN_VALUE;
			}
		}
	}
//	打印螺旋值
	public void printSpiral() {
//		行列1时打印螺旋值
		if(this.row==1||this.col==1) {
			printArray();
			return;
		}
		while(size-->0) {
			printData();
			saveData();
			setRange();
			if(this.rowIndex==this.rowStart&&this.colIndex<this.colEnd) {
				toRight();
			}else if(this.rowIndex<this.rowEnd&&this.colIndex==this.colEnd) {
				toDown();
			}else if(this.rowIndex==this.rowEnd&&this.colIndex>this.colStart) {
				toLeft();
			}else if(this.rowIndex>this.rowStart&&this.colIndex==this.colStart) {
				toUp();
			}
		}
	}
//	设置行列开始结束值
	private void setRange() {
//		开始结束值的界分
		int rowDivide = this.row/2;
		int colDivide = this.col/2;
//		判断一行每个元素是否都走完
		for(int i=0;i<this.row;i++) {
			int colCount = 0;
			int[] r = this.saveArray[i];
			for(int j=0;j<this.col;j++) {
				int c = r[j];
				if(c!=Integer.MIN_VALUE) {
					++colCount;
				}
			}
			if(colCount==this.col) {
				if(i+1<rowDivide) this.rowStart = i + 1;
				if(i-1>=rowDivide) this.rowEnd = i - 1;
			}
		}
//		判断列元素是否走完
		for(int i=0;i<this.col;i++) {
			int rowCount = 0;
			for(int j=0;j<this.row;j++) {
				int data = this.saveArray[j][i];
				if(data!=Integer.MIN_VALUE) {
					++rowCount;
				}
			}
			if(rowCount==this.row) {
				if(i+1<colDivide) this.colStart = i + 1;
				if(i-1>=colDivide) this.colEnd = i - 1;
			}
		}
	}
	
	//	打印数组
	private void printArray() {
		for(int[] arr : this.spiralArray) {
			for(int data : arr) {
				System.out.print(data);
			}
		}
	}
//	打印元素
	private void printData() {
		System.out.print(this.spiralArray[this.rowIndex][this.colIndex]);
	}
//	向上移动
	private void toUp() {
		this.rowIndex = (this.rowIndex-1)>=0?--this.rowIndex:0;
	}
//	向下移动
	private void toDown() {
		this.rowIndex = (this.rowIndex+1)<this.row?++this.rowIndex:this.row-1;
	}
//	向左移动
	private void toLeft() {
		this.colIndex = (this.colIndex-1)>=0?--this.colIndex:0;
	}
//	向右移动
	private void toRight() {
		this.colIndex = (this.colIndex+1)<this.col?++this.colIndex:this.col-1;
	}
//	保存已经过数据
	private void saveData() {
		this.saveArray[this.rowIndex][this.colIndex] = this.spiralArray[this.rowIndex][this.colIndex];
	}
}

测试类

java 复制代码
package test;

import java.util.Arrays;

import entity.Spiral;

public class SpiralTest {

	public static void main(String[] args) {
//		int row = 1;
//		int col = 1;
//		int[][] array = {{1}};
//		int row = 1;
//		int col = 2;
//		int[][] array = {{1,2}};
//		int row = 2;
//		int col = 1;
//		int[][] array = {{1},{2}};
		int row = 4;
		int col = 5;
		int[][] array = new int[row][col];
		for(int[] r : array) {
			for(int i=0;i<r.length;i++) {
				r[i] = (int)(Math.random()*100);
			}
		}
		System.out.println(Arrays.deepToString(array));
		Spiral sp = new Spiral(row,col,array);
		sp.printSpiral();
	}

}
相关推荐
___Dream2 分钟前
codetop+acwing加减乘除法与位运算的总结,别再饶头啦!!!
数据结构·c++·算法·高精度
后端小肥肠7 分钟前
【Spring Security系列】如何用Spring Security集成手机验证码登录?五分钟搞定!
java·spring boot·后端·spring
龟四崛起25 分钟前
你的绩效是不是常年都是B
java·经验分享·程序人生·职场和发展·职场发展
⠀One0ne25 分钟前
软件设计原则(Java实现/给出正例反例)
java·软件工程
TSINGSEE39 分钟前
人员抽烟AI检测算法在智慧安防领域的创新应用,助力监控智能化
人工智能·算法·视频编解码·安防视频监控·视频监控管理平台
拾木2001 小时前
常见的限流算法
java·开发语言
处处清欢1 小时前
MaintenanceController
java·开发语言
飞翔的佩奇1 小时前
Java项目: 基于SpringBoot+mybatis+maven校园资料分享平台(含源码+数据库+答辩PPT+毕业论文)
java·spring boot·spring·毕业设计·maven·mybatis·校园资料分享平台
不平衡的叉叉树1 小时前
Java对象列表属性映射工具类
java
_feivirus_1 小时前
神经网络_使用TensorFlow预测气温
人工智能·神经网络·算法·tensorflow·预测气温