package view;
/**
* 面试
* 输入一个矩阵,按照从外向里以顺时针/逆时针的顺序依次打印出每一个数字。
* 例;
* 1 ,2 ,3 ,4
* 5 ,6 ,7 ,8
* 10,9 ,0 ,-1
* 11,13,12,14
* sout:
* 1,5,10,11,13,12,14,-1,8,4,3,2,6,9,0,7
* @author zlz on 2021/3/26
*/
public class MainTest {
/**
* 顺时针输出
*/
private static int CLOCKWISE = 1;
/**
* 逆时针输出
*/
private static int ANTICLOCKWISE = 2;
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4}, {5,6,7,8}, {10,9,0,-1}, {11,13,12,14}};
soutMatrix(matrix,"原始矩阵");
StringBuilder builder = new StringBuilder();
// 按方向输出矩阵
print(matrix, ANTICLOCKWISE, builder);
}
/**
* 输出矩阵中的元素
* @param matrix int类型的矩阵
* @param builder
*/
private static void print(int[][] matrix, int direction, StringBuilder builder) {
// 若是矩阵中的元素全部移除则输出builder
if(matrix.length == 0){
String out = builder.substring(0, builder.length() - 1);
System.out.println("[" + out + "]");
return;
}
// 顺时针输出
if(direction == CLOCKWISE){
// 获取top
matrix = getTop(matrix, direction, builder);
// 获取right
matrix = getRight(matrix, direction, builder);
// 获取buttom
matrix = getButtom(matrix, direction, builder);
// 获取left
matrix = getLeft(matrix, direction, builder);
}else {
// 获取left
matrix = getLeft(matrix, direction, builder);
// 获取buttom
matrix = getButtom(matrix, direction, builder);
// 获取right
matrix = getRight(matrix, direction, builder);
// 获取top
matrix = getTop(matrix, direction, builder);
}
// 递归调用
print(matrix, direction, builder);
}
/**
* 输出矩阵上方位置的字符
* @param matrix
* @param direction
* @return
*/
private static int[][] getTop(int[][] matrix, int direction, StringBuilder builder) {
// 矩阵中没有元素
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "上方处理");
// 获取矩阵最上方的元素
int[] arr = matrix[0];
// 顺时针输出
if(direction == CLOCKWISE){
// 从头获取
for (int i = 0; i < arr.length; i++) {
builder.append(arr[i]).append(",");
}
}else {
// 从尾获取
for (int i = arr.length - 1; i > -1; i--) {
builder.append(arr[i]).append(",");
}
}
// 处理矩阵将获取到的部分移除
int[][] newMatrix = new int[matrix.length-1][arr.length];
System.arraycopy(matrix, 1, newMatrix, 0, matrix.length - 1);
// 返回处理之后的矩阵
return newMatrix;
}
/**
* 输出矩阵右方位置的字符
* @param matrix
* @param direction
* @return
*/
private static int[][] getRight(int[][] matrix, int direction, StringBuilder builder) {
// 矩阵中没有元素
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "右侧处理");
// 顺时针输出
if(direction == CLOCKWISE){
// 从上获取
for (int[] ints : matrix) {
builder.append(ints[ints.length - 1] + ",");
}
}else {
// 从下获取
for (int i = matrix.length - 1; i > -1; i--) {
builder.append(matrix[i][matrix[i].length - 1] + ",");
}
}
// 处理矩阵将获取到的部分移除
int[][] newMatrix = new int[matrix.length][matrix[0].length - 1];
for (int i = 0; i < matrix.length; i++) {
if (matrix[i].length - 1 >= 0) {
System.arraycopy(matrix[i], 0, newMatrix[i], 0, matrix[i].length - 1);
}
}
// 返回处理之后的矩阵
return newMatrix;
}
/**
* 输出矩阵左方位置的字符
* @param matrix
* @param direction
* @return
*/
private static int[][] getLeft(int[][] matrix, int direction, StringBuilder builder) {
// 矩阵中没有元素
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "左侧处理");
// 顺时针输出
if(direction == CLOCKWISE){
// 从下获取
for (int i = matrix.length - 1; i > -1; i--) {
builder.append(matrix[i][0] + ",");
}
}else {
// 从上获取
for (int[] ints : matrix) {
builder.append(ints[0] + ",");
}
}
// 处理矩阵将获取到的部分移除
int[][] newMatrix = new int[matrix.length][matrix[0].length - 1];
for (int i = 0; i < matrix.length; i++) {
if (matrix[i].length - 1 >= 0) {
System.arraycopy(matrix[i], 1, newMatrix[i], 0, matrix[i].length - 1);
}
}
// 返回处理之后的矩阵
return newMatrix;
}
/**
* 输出矩阵下方位置的字符
* @param matrix
* @param direction
* @return
*/
private static int[][] getButtom(int[][] matrix, int direction, StringBuilder builder) {
// 矩阵中没有元素
if(matrix.length == 0){
return matrix;
}
soutMatrix(matrix, "下方处理");
// 获取矩阵最下方的元素
int[] arr = matrix[matrix.length - 1];
// 顺时针输出
if(direction == CLOCKWISE){
// 从尾获取
for (int i = arr.length - 1; i > -1; i--) {
builder.append(arr[i]).append(",");
}
}else {
// 从头获取
for (int i = 0; i < arr.length; i++) {
builder.append(arr[i]).append(",");
}
}
// 处理矩阵将获取到的部分移除
int[][] newMatrix = new int[matrix.length-1][arr.length];
System.arraycopy(matrix, 0, newMatrix, 0, matrix.length - 1);
// 返回处理之后的矩阵
return newMatrix;
}
/**
* 输出矩阵
* @param matrix 矩阵
* @param note 备注
*/
private static void soutMatrix(int[][] matrix, String note){
System.out.println(note);
for (int i = 0; i < matrix.length; i++) {
for (int i1 = 0; i1 < matrix[i].length; i1++) {
if(i1 == matrix[i].length -1){
System.out.print(matrix[i][i1]);
}else {
System.out.print(matrix[i][i1] + ",");
}
}
System.out.println();
}
System.out.println("-------------------------");
}
}
算法-输出矩阵Java代码版
最新推荐文章于 2022-04-27 17:32:14 发布