顺时针打印矩阵

本文介绍了一种顺时针打印矩阵的方法,通过控制四个边界坐标(左、右、上、下),实现从外向内逐层打印矩阵元素。适用于行数M和列数N在1至99之间的矩阵。

顺时针打印矩阵各元素

输入:矩阵的行数M、列数N、矩阵各元素的值
输出:有外到内,按顺时针打印矩阵

/* The snake matrix: output a two-dimensional matrix, the matrix elements are output from the outside to the inside
 * The direction is made: up north down south, left west right east
 * The direction of each cycle is: 1 to the east 2 to the south 3 to the west 4 to the north

   * * * * * * * * * * 
   *             *   *
   * * * * * * * *   *
   *   *         *   *
   *   *         *   *
   *   *         *   *
   *   *         *   *
   *   * * * * * * * *
   *   *             *
   * * * * * * * * * *  

 * rectangle has four coordinates:left,right,up,down

*/


import java.util.Scanner;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        try{
            int M = input.nextInt();//rows
            int N = input.nextInt();//columns

            if(M>0 && M<100 && N>0 && N<100){
                if(M ==1){
                    int[] arr = new int [N];
                    for(int j=0;j<N;j++)
                        arr[j] = input.nextInt();
                    for(int j=0;j<N;j++)
                        System.out.print(arr[j]+" ");
                    System.out.println();               
                }
                else if(N ==1 && M !=1){
                    int[] arr = new int [M];
                    for(int i=0;i<M;i++)
                        arr[i] = input.nextInt();
                    for(int i=0;i<M;i++)
                        System.out.print(arr[i]+" ");
                    System.out.println();                   
                }               
                else{
                    int[][] array = new int[M][N];
                    for(int i=0;i<M;i++){
                        for(int j=0;j<N;j++){
                            array[i][j] = input.nextInt();
                        }
                    }

                    int count = 0;
                    int left = 0;
                    int right = N;
                    int up = 0;
                    int down = M;
                    while(count < (M*N)){
                        for(int j=left;j<right-1 && left<right;j++){
                            System.out.print(array[up][j]+" ");
                            count++;
                            if(count>=(M*N))
                                break;
                        }
                        for(int i=up;i<down-1 && up<down;i++){
                            System.out.print(array[i][right-1]+" ");
                            count++;
                            if(count>=(M*N))
                                break;
                        }
                        for(int j=right-1;j>left && left<right;j--){
                            System.out.print(array[down-1][j]+" ");
                            count++;
                            if(count>=(M*N))
                                break;
                        }
                        for(int i=down-1;i>up && up<down;i--){
                            System.out.print(array[i][left]+" ");
                            count++;
                            if(count>=(M*N))
                                break;
                        }
                        left++;
                        right--;
                        up++;
                        down--;
                    }

                }
            }
            else
                System.out.println("Input error!");

        }finally{
            input.close();
        }


    }

}

### 顺时针打印矩阵的实现 在JavaScript中,顺时针打印矩阵是一种常见的算法问题。该问题要求我们按照从外到内的顺序逐层打印矩阵的元素。为了实现这一功能,可以采用循环遍历的方式,依次处理矩阵的上边、右边、下边和左边。 #### 算法思路 1. **初始化边界**:定义四个变量来记录当前层的上下左右边界。 2. **按层遍历**:每一层分为四个方向进行遍历: - 从左到右遍历上边。 - 从上到下遍历右边。 - 从右到左遍历下边(如果存在)。 - 从下到上遍历左边(如果存在)。 3. **更新边界**:每完成一层的遍历后,更新相应的边界值,继续处理内层。 4. **终止条件**:当所有元素都被遍历完后,退出循环。 #### JavaScript 实现代码 以下是一个完整的JavaScript函数,用于顺时针打印矩阵: ```javascript function spiralOrder(matrix) { if (!matrix || matrix.length === 0) { return []; } let result = []; let top = 0; let bottom = matrix.length - 1; let left = 0; let right = matrix[0].length - 1; while (top <= bottom && left <= right) { // 从左到右 for (let i = left; i <= right; i++) { result.push(matrix[top][i]); } top++; // 从上到下 for (let i = top; i <= bottom; i++) { result.push(matrix[i][right]); } right--; // 从右到左 if (top <= bottom) { for (let i = right; i >= left; i--) { result.push(matrix[bottom][i]); } bottom--; } // 从下到上 if (left <= right) { for (let i = bottom; i >= top; i--) { result.push(matrix[i][left]); } left++; } } return result; } ``` #### 示例使用 假设有一个如下的二维数组: ```javascript const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; console.log(spiralOrder(matrix)); // 输出: [1, 2, 3, 6, 9, 8, 7, 4, 5] ``` 这段代码会正确地输出矩阵顺时针遍历结果。 ### 性能优化与复杂度分析 - **时间复杂度**:O(m * n),其中 `m` 是行数,`n` 是列数。每个元素都会被访问一次。 - **空间复杂度**:O(1),除了存储结果的数组外,没有使用额外的空间。 通过这种方式,可以在JavaScript中高效地实现顺时针打印矩阵的功能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值