zoukankan      html  css  js  c++  java
  • python剑指offer 顺时针打印指针

    题目描述

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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.
    # -*- coding:utf-8 -*-
    class Solution:
        # matrix类型为二维列表,需要返回列表
        def printMatrix(self, matrix):
            # write code here
            if not matrix:
                return None
            rows = len(matrix)
            cols = len(matrix[0])
            start = 0
            result = []
            while rows > 2*start and cols > 2*start:
                endx = rows-1-start
                endy = cols-1-start
                for i in range(start,endy+1):
                    result.append(matrix[start][i])
                if start < endx:
                    for i in range(start+1,endx+1):
                        result.append(matrix[i][endy])
                if start < endx and start < endy:
                    for i in range(endy-1,start-1,-1):
                        result.append(matrix[endx][i])
                if start < endx-1 and start < endy:
                    for i in range(endx-1,start,-1):
                        result.append(matrix[i][start])
                         
                start += 1
            return result
    
  • 相关阅读:
    码云的安装和配置
    Python解释器安装教程和环境变量配置
    Python基础学习
    buuctf CheckIn
    各种小马收集
    buuctf Easysql 小记
    locust
    封装好的日志模块
    实用的测试网站
    列表操作
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/9685413.html
Copyright © 2011-2022 走看看