Leetcode每日一题-螺旋矩阵

【题目描述】

给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:

m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrixi <= 100

【分析】

  • 思路:

    首先打印矩阵周围一圈,再对矩阵进行切片,成为一个新的矩阵,再对新矩阵进行打印,有点递归的感觉。

    切片代码:matrix = [matrix_[c:m_-c][i][c:n_-c] for i in range(m_-c*2)]

  • AC代码:

    class Solution(object):
    def spiralOrder(self, matrix_):
    """
    :type matrix: List[List[int]]
    :rtype: List[int]
    """
    output = []
    m_ = len(matrix_)
    n_ = len(matrix_[0])
    cycle = m_ if m_ < n_ else n_
    for c in range((cycle+1)//2):
    matrix = [matrix_[c:m_-c][i][c:n_-c] for i in range(m_-c*2)]
    #print(matrix)
    n = len(matrix[0])
    m = len(matrix)
    for i in range(n):
    output.append(matrix[0][i])
    for i in range(m-1):
    output.append(matrix[i+1][n-1])
    for i in range(n-1):
    if(m-1==0):
    break
    output.append(matrix[m-1][n-i-2])
    for i in range(m-2):
    if(n-1==0):
    break
    output.append(matrix[m-i-2][0])
    return output
  • 讨论:

    • java版的0ms代码:

      class Solution {
      public List<Integer> spiralOrder(int[][] matrix) {
      List<Integer> ans = new ArrayList<>();
      int[] pos = { 0, 0 };
      // 上 右 下 左
      int[] ract = { 0, matrix[0].length - 1, matrix.length - 1, 0, };
      // d = 0,1,2,3时 分别代表往右、往下、往左、往上
      int d = 0;
      while (ans.size() < matrix.length * matrix[0].length) {
      ans.add(matrix[pos[0]][pos[1]]);
      if (d == 0 && pos[1] < ract[1]){
      pos[1] ++;
      continue;
      }
      if (d == 0 && pos[1] == ract[1]){
      pos[0] ++;
      ract[d] ++;
      d = (d + 1) % 4 ;
      continue;
      }
      if (d == 1 && pos[0] < ract[2]){
      pos[0] ++;
      continue;
      }
      if (d == 1 && pos[0] == ract[2]){
      pos[1] --;
      ract[d] --;
      d = (d + 1) % 4 ;
      continue;
      }
      if (d == 2 && pos[1] > ract[3]){
      pos[1] --;
      continue;
      }
      if (d == 2 && pos[1] == ract[3]){
      pos[0] --;
      ract[d] --;
      d = (d + 1) % 4 ;
      continue;
      }
      if (d == 3 && pos[0] > ract[0]){
      pos[0] --;
      continue;
      }
      if (d == 3 && pos[0] == ract[0]){
      pos[1] ++;
      ract[d] ++;
      d = (d + 1) % 4 ;
      continue;
      }
      }
      return ans;
      }
      }
    • 螺旋向量的转移方程:

      di, dj = 0, 1
      di, dj = dj, -di

      取自点赞量最高的讨论

    • 官方题解: 戳这里

【python】Leetcode每日一题-螺旋矩阵的更多相关文章

  1. 【python】Leetcode每日一题-螺旋矩阵2

    [python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...

  2. 【python】Leetcode每日一题-矩阵置零

    [python]Leetcode每日一题-矩阵置零 [题目描述] 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 .请使用 原地 算法. 进阶: 一个直观的解 ...

  3. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  4. 【python】Leetcode每日一题-删除有序数组中的重复项

    [python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...

  5. 【python】Leetcode每日一题-存在重复元素3

    [python]Leetcode每日一题-存在重复元素3 [题目描述] 给你一个整数数组 nums 和两个整数 k 和 t .请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] ...

  6. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  7. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  8. 【python】Leetcode每日一题-打家劫舍2

    [python]Leetcode每日一题-打家劫舍2 [题目描述] 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋 ...

  9. 【python】Leetcode每日一题-二叉搜索树节点最小距离

    [python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...

随机推荐

  1. PHP配置 2. 日志相关配置

    例如,在disable_functions,定义禁用phpinfo函数, # vim /usr/local/php/etc/php.ini disable_functions=phpinfo,eval ...

  2. org.springframework.web.util.IntrospectorCleanupListener作用

    回收那些不会主动回收,导致内存泄漏的垃圾,如javabeans

  3. weex参考文章

    1官网:https://weex.apache.org/zh/guide/introduction.html 2.weexui   https://alibaba.github.io/weex-ui/ ...

  4. golang 微服务以及相关web框架

    golang 中国gocn golang Applicable to all database connection pools xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常 ...

  5. ch1_5_2求无序序列中第k小的元素

    import java.util.Arrays; import java.util.PriorityQueue; public class ch1_5_2求无序序列中第k小的元素 { public s ...

  6. [go-linq]-Go的.NET LINQ式查询方法

    关于我 我的博客|文章首发 开发者的福音,go也支持linq了 坑爹的集合 go在进行集合操作时,有很不舒服的地方,起初我真的是无力吐槽,又苦于找不到一个好的第三方库,只能每次写着重复代码.举个栗子 ...

  7. JS实现鼠标点击爱心&绘制多边形&每日一言功能

    本篇文章主要介绍我的个人博客 程序猿刘川枫 中页面使用的美化功能(基于JS实现): 1.鼠标点击出现不同颜色爱心特效 2.页面浮动多边形跟随鼠标移动 3.每日一言功能 1.鼠标点击出现爱心特效 经常在 ...

  8. WordPress的SEO优化技巧

    世界上大约有30%的网站都是由Wordpress搭建的,因为Wordpress自身构架清晰,代码规范,且网页评论直接书写在整个页面里,能够被搜索引擎检索到,因此对搜索引擎很友好.但有时候还是会出现只被 ...

  9. C#补位函数PadLeft和PadRight

    左边补位 PadLeft 用法: string str = "100"; str.PadLeft(5,'0') 输出:00100 右边补位 PadRight 用法: str.Pad ...

  10. OO第三单元小结

    目录 JML理论基础 JML工具链 openjml使用 openjml总结 jmlunitng使用 代码分析 第一次作业 第二次作业 第三次作业 测试&bug分析 黑盒测试 白盒测试(Juni ...