二维数组顺时针遍历

二维数组顺时针遍历

1、题目要求

From PDD

  1. 给你一个 m*n 的二维数组 arr[][],按照顺时针遍历输出

  2. 比如下面的数组,其输出顺序应该为 1~15

    int[][] arr = new int[][]{
        {1, 2, 3, 4, 5},
        {12, 13, 14, 15, 6},
        {11, 10, 9, 8, 7},
    };
    

2、代码思路

这道题并不难,使用两个变量 row 和 col 分别作为行、列指针,使用两个变量 xLeft 和 xRight 维护左右边界,使用两个变量 yUp 和 yDown 维护上下边界,使用一个变量 direction 维护当前遍历的方向

3、代码实现

  1. 代码

    /**
     * @ClassName ArrayClockwiseIter
     * @Description TODO
     * @Author Heygo
     * @Date 2020/8/16 12:43
     * @Version 1.0
     */
    public class ArrayClockwiseIter {
    
        public static void main(String[] args) {
            int[][] arr = new int[][]{
                    {1, 2, 3, 4, 5},
                    {12, 13, 14, 15, 6},
                    {11, 10, 9, 8, 7},
            };
            clockwiseIter(arr);
        }
    
        /**
         * 顺时针遍历输出二维数组
         * @param arr 待遍历输出的二维数组
         */
        public static void clockwiseIter(int[][] arr) {
    
            // 行、列指针
            int row = 0;
            int col = 0;
    
            // 列的左右边界
            int xLeft = 0;
            int xRight = arr[0].length - 1;
    
            // 行的上下边界
            int yUp = 0;
            int yDown = arr.length - 1;
    
            // 已遍历输出的元素个数
            int count = 0;
            int totalCount = arr.length * arr[0].length;
    
            // 行走方向:0 表示从左往右,1 表示从上往下,2 表示从右往左,3 表示从下往下
            int direction = 0;
    
            while (count < totalCount) {
                if (direction == 0) {
                    for (col = xLeft; col <= xRight; col++, count++) {
                        System.out.print(arr[row][col]+" ");
                    }
                    System.out.println();
                    yUp += 1; // 上边界
                    direction = 1; // 变换方向
                    col--;
                } else if (direction == 1) {
                    for (row = yUp; row <= yDown; row++, count++) {
                        System.out.print(arr[row][col]+" ");
                    }
                    System.out.println();
                    xRight -= 1; // 右边界
                    direction = 2; // 变换方向
                    row--;
                } else if (direction == 2) {
                    for (col = xRight; col >= xLeft; col--, count++) {
                        System.out.print(arr[row][col]+" ");
                    }
                    System.out.println();
                    yDown -= 1; // 下边界
                    direction = 3; // 变换方向
                    col++;
                } else if (direction == 3) {
                    for (row = yDown; row >= yUp; row--, count++) {
                        System.out.print(arr[row][col]+" ");
                    }
                    System.out.println();
                    xLeft += 1; // 左边界
                    direction = 0; // 变换方向
                    row++;
                }
            }
    
        }
    
    }
    
  2. 程序运行结果

    1 2 3 4 5 
    6 7 
    8 9 10 11 
    12 
    13 14 15 
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值