博客
关于我
顺时针打印矩阵
阅读量: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/

你可能感兴趣的文章
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>