16.转圈打印矩阵

转圈打印矩阵

举例3个矩阵自行手动转圈打印,梳理逻辑过程

在这里插入图片描述

思路也极其简单:

先记录右、下、左、上边界

从左上角第一个元素往右走打印到右边界就往下走,遇到下边界就往左走;遇到左边界往上走如此循环往复

往右、往下、往左、往上分别走到边界,上、右、下、左边界要向中心位置缩一行
右、下、左、上循环打印,边界不断内缩

循环什么时候结束需要看打印的元素个数是否是矩阵的个数

import java.util.*;


public class Solution {
    /**
     * 
     * @param matrix int整型二维数组 the matrix
     * @return int整型一维数组
     */
    public int[] printMatrix (int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        int left = 0;
        int right = col-1;
        int top = 0;
        int floor = row - 1;
        int count = 0;
        int[] res = new int[row*col];
        while(count<=row*col){
            //往右
            for(int i=left; i<=right; ++i){
                //System.out.println(matrix[top][i]);
                res[count++] = matrix[top][i];
            }
            if(count == row*col) return res;
            top++;
            //往下
            for(int i=top; i<=floor; ++i){
                //System.out.println(matrix[i][right]);
                res[count++] = matrix[i][right];
            }
            if(count == row*col) return res;
            right--;
            //往左
            for(int i=right; i>=left; --i){
                //System.out.println(matrix[floor][i]);
                res[count++] = matrix[floor][i];
            }
            if(count == row*col) return res;
            floor--;
            //往上
            for(int i=floor; i>=top; --i){
                //System.out.println(matrix[i][left]);
                res[count++] = matrix[i][left];
            }
            left++;
        }
        return res;
    }
}
改进
import java.util.*;


public class Solution {
    /**
     * 
     * @param matrix int整型二维数组 the matrix
     * @return int整型一维数组
     */
    public int[] printMatrix (int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        int left = 0;
        int right = col-1;
        int top = 0;
        int floor = row - 1;
        int count = 0;
        int[] res = new int[row*col];
        while(left<right && top<floor){
            //往右
            for(int i=left; i<=right; ++i)
                res[count++] = matrix[top][i];
            top++;
            //往下
            for(int i=top; i<=floor; ++i)
                res[count++] = matrix[i][right];
            right--;
            //往左
            for(int i=right; i>=left; --i)
                res[count++] = matrix[floor][i];
            floor--;
            //往上
            for(int i=floor; i>=top; --i)
                res[count++] = matrix[i][left];
            left++;
        }
        if(left == right) //往下
            for (int i = top; i <= floor; ++i)
                res[count++] = matrix[i][right];
        else if(top == floor) //往右
            for(int i=left; i<=right; ++i)
                res[count++] = matrix[top][i];
        return res;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值