剑指Offer:顺时针打印矩阵Java/Python

本文介绍了一种矩阵顺时针打印的算法,包括两种方法:一是通过模拟顺时针过程,二是通过输出并删除矩阵的第一行,然后逆时针旋转矩阵。详细解释了算法步骤,提供了Java和Python代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

2.算法描述

方法1:模拟顺时针过程。

1.初始化left,right,top,bottom分别代表左、右、上、下的边界。
2.当left<right 并且 top<bottom时:
left->right,从左到右扫描矩阵元素;
top->bottom,从上到下扫描矩阵元素;
right->left,从右到左扫描矩阵元素;
bottom->top,从下到上扫描矩阵元素。
一次顺时针完成,需要缩小左、右、上、下的边界,left+1,top+1,right-1, bottom-1.
3.当2的条件不满足时:
如果left=right 并且 top<bottom,说明只有一列,需要从上到下扫描矩阵元素;
如果top=bottom并且left<right,说明只有一行,需要从左到右扫描矩阵元素;
否则只有一个元素

方法2:看了下大神的思路,果然不一样啊,感叹自身的渣。

思路如下:
过程可以是这样的两步,直到矩阵为空:
1.输出矩阵的第一行,并删除
2.如果矩阵为空,打印完毕;如果矩阵不为空,将矩阵逆时针旋转90度
例如
1 2 3
4 5 6
7 8 9
输出并删除第一行后,再进行一次逆时针旋转,就变成:
6 9
5 8
4 7
继续重复上述操作即可。
注意:如何将矩阵逆时针旋转90度?\red{如何将矩阵逆时针旋转90度?}90
4 5 6
7 8 9
1.转置\red{1.转置}1.
4 7
5 8
6 9
2.以行为单位逆序\red{2.以行为单位逆序}2.
6 9
5 8
4 7
用Python实现起来很简单。具体见代码。

3.代码描述

3.1.Java代码

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> ans = new ArrayList<>();
        int left=0,right=matrix[0].length-1,top=0,bottom= matrix.length-1;
        while(left<right && top<bottom){
            for(int i=left; i<=right; i++){//左右
                ans.add(matrix[top][i]);
            }
            for(int i=top+1;i<=bottom;i++){//上下
                ans.add(matrix[i][right]);
            }
            for(int i=right-1;i>=left;i--){//右左
                ans.add(matrix[bottom][i]);
            }
            for(int i=bottom-1;i>top;i--){//下上
                ans.add(matrix[i][left]);
            }
            //缩小边界
            top++;
            bottom--;
            left++;
            right--;
        }
        if(top == bottom && left < right){//只有一行
            for(int i=left;i<=right;i++)
                ans.add(matrix[top][i]);
        }
        if(left == right && top < bottom){//只有一列
            for(int i=top;i<=bottom;i++)
                ans.add(matrix[i][left]);
        }
        if(left == right && top == bottom){
            ans.add(matrix[left][top]);
        }
        return ans;
    }
}

3.2.Python代码

# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表,需要返回列表
    def printMatrix(self, matrix):
        # write code here
        ans = []
        while matrix:
            ans.extend(matrix.pop(0))
            if not matrix or not matrix[0]:
                break
            matrix = self.rotate90(matrix)
        return ans
    def rotate90(self, matrix):
        rows = len(matrix)
        cols = len(matrix[0])
        newMatrix = []
        #以下代码将矩阵转置
        for c in range(cols):
            subcol = []
            for r in range(rows):
                subcol.append(matrix[r][c])
            newMatrix.append(subcol)
        #现在是将矩阵逆时针旋转90度
        newMatrix.reverse()
        return newMatrix
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值