博客
关于我
顺时针打印矩阵
阅读量:338 次
发布时间:2019-03-03

本文共 1184 字,大约阅读时间需要 3 分钟。

在这里插入图片描述

难度竟然是简单,我错了好几次。。。。。。

# -*- coding:utf-8 -*-class Solution:    # matrix类型为二维列表,需要返回列表    def printMatrix(self, matrix):        # write code here        res = []        if len(matrix) == 0:            return res        top = 0        bottom = len(matrix) - 1        left = 0        right = len(matrix[0]) - 1        while True:            for i in range(left, right + 1):                res.append(matrix[top][i])            top  = top + 1            if top > bottom: break;            for i in range(top, bottom +1):                res.append(matrix[i][right])            right = right - 1            if right < left: break;            for i in range(right, left-1, -1):                res.append(matrix[bottom][i])            bottom = bottom - 1            if bottom < top: break;            for i in range(bottom, top-1, -1):                res.append(matrix[i][left])            left = left + 1            if left > right: break;                    return res

**

奇淫技巧

**

class Solution:    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:        res = []        while matrix:            res += matrix.pop(0)            matrix = list(zip(*matrix))[::-1]        return res

转载地址:http://cysl.baihongyu.com/

你可能感兴趣的文章
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>