Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7] 思路

    这到题的思路不难,但是在边界的处理上非常复杂。自己做了好一会也没能ac,看了以下solution写的也是听复杂度,最后看到了一个简单易懂的答案。具体思路是 我们设置一个up、down、left、right变量,来进行遍历。循环的条件就是up < down and left < right ,(and 是防止出现3*2 这种矩阵,因为下面还要继续进行殊处理) 每次循环完毕之后up, left加1, down, right减1。循环内部进行四次循环,分别添加每一层的数字。直到外层循环结束。最后在判断特殊的比如2*3这种矩阵。时间复杂度为O(m*n), 空间复杂度为O(N)(N为元素总个数)
解决代码


 class Solution(object):
def spiralOrder(self, nums):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not nums:
return [] m , n = len(nums), len(nums[0])
up, down, left, right = 0, m-1, 0, n-1 # 设置上下左右变量。
res = []
while up < down and left < right: # 循环条件
for i in range(left, right): # 打印当前层的上半部分
res.append(nums[up][i]) for i in range(up, down): # 打印当前层的右半部部分
res.append(nums[i][right]) for i in range( right, left,-1): # 打印当前层的下边部分
res.append(nums[down][i])
for i in range(down, up, -1): # 打印当前层的左半部分
res.append(nums[i][left])
up, down, left, right = up+1, down-1, left+1, right-1 if left == right: # 如果左等于右,说明可能存在2*3 这种形式的矩阵,将最里面的一层添加
res.extend([nums[i][right] for i in range(up, down + 1)])
elif up == down: # 如果上等于下, 说明可能存在3*2 这种类型的矩阵。
res.extend([nums[up][j] for j in range(left, right + 1)])
return res

【LeetCode每天一题】Spiral Matrix(螺旋打印数组)的更多相关文章

  1. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  2. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

  3. 【js】Leetcode每日一题-解码异或后数组

    [js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...

  4. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

  5. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  6. 【python】【补】Leetcode每日一题-合并两个有序数组

    [python]Leetcode每日一题-合并两个有序数组 [题目描述] 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组 ...

  7. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  9. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

随机推荐

  1. 干货 | Elasticsearch 集群健康值红色终极解决方案【转】

    题记 Elasticsearch当清理缓存( echo 3 > /proc/sys/vm/drop_caches )的时候,出现 如下集群健康值:red,红色预警状态,同时部分分片都成为灰色.  ...

  2. hdoj:2056

    #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; str ...

  3. thinkphp5 composer

    前提:已安装composer 1.安装包 https://packagist.org/?query=thinkphp ,tp的各种安装包 2.安装 //安装命令, composer create-pr ...

  4. TCP连接的状态与关闭方式,及其对Server与Client的影响

    1. TCP连接的状态 首先介绍一下TCP连接建立与关闭过程中的状态.TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用.特定数据包以及超时等,具体状态如下所示: CLOSED:初始状态, ...

  5. swiper4自动轮播切换手动触碰后停止踩坑——属性disableOnInteraction

    swiper4轮播设置autoplay自动切换后,即默认设置: <script> var mySwiper = new Swiper('.swiper-container', { auto ...

  6. opencv利用直方图判断人脸光照质量

    懒得用中文再写一遍了, 直接传送门过去吧. https://medium.com/@fanzongshaoxing/detect-face-in-bad-lighting-condition-usin ...

  7. x264_param_default分析

    {     /* 开辟内存空间*/     memset( param, 0, sizeof( x264_param_t ) );              /* CPU自动检测 */     par ...

  8. vue 使用font-awesome

    1.npm 安装font-awesome 以及需要的所有依赖 npm i --save @fortawesome/fontawesome-svg-core npm i --save @fortawes ...

  9. deep learning 以及deep learning 常用模型和方法

    首先为什么会有Deep learning,我们得到一个结论就是Deep learning需要多层来获得更抽象的特征表达. 1.Deep learning与Neural Network 深度学习是机器学 ...

  10. Ubuntu下安装LNMP

    1.安装mysql sudo apt-get install mysql-server mysql-client 2.安装nginx sudo apt-get install nginx 安装完后重启 ...