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

Example 1:

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

Example 2:

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

题意:

按螺旋方式遍历矩阵。

Solution1: Simulation the process and implement it.

code

  1. class Solution {
  2. public List<Integer> spiralOrder(int[][] matrix) {
  3. List<Integer> res = new ArrayList<>();
  4. if(matrix.length == 0 || matrix[0].length == 0) return res;
  5.  
  6. int top = 0;
  7. int bottom = matrix.length-1;
  8. int left = 0;
  9. int right = matrix[0].length-1;
  10.  
  11. while(true){
  12. for(int i = left; i <= right; i++) {
  13. res.add(matrix[top][i]);
  14. }
  15. top++;
  16. if(left > right || top > bottom) break;
  17.  
  18. for(int i = top; i <= bottom; i++) {
  19. res.add(matrix[i][right]);
  20. }
  21. right--;
  22. if(left > right || top > bottom) break;
  23.  
  24. for(int i = right; i >= left; i--) {
  25. res.add(matrix[bottom][i]);
  26. }
  27. bottom--;
  28. if(left > right || top > bottom) break;
  29.  
  30. for(int i = bottom; i >= top; i--){
  31. res.add(matrix[i][left]);
  32. }
  33. left++;
  34. if(left > right || top > bottom) break;
  35. }
  36. return res;
  37. }
  38. }

[leetcode]54. 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 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

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

  3. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  6. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  7. LeetCode 54. 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] Spiral Matrix 螺旋矩阵

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

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

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

随机推荐

  1. MySQL Error--The Table is full

    问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...

  2. 算法笔记 3.2 codeup1934 找X

    #include <stdio.h> ; int a[maxn]; int main(void){ int n; while(scanf("%d", &n)!= ...

  3. SQL 查询当天,本月,本周的记录

    SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111)   ORDE ...

  4. forward reference前向引用,gloal values and local values全局变量和局部变量,recursive function递归函数

    1.全局变量与局部变量练习 1 # -*- coding: UTF-8 -*- 2 def bar(): 3 print('from bar') 4 def foo(): 5 print('from ...

  5. 黄聪:如何扩展Chrome DevTools来获取页面请求

    1. Chrome DevTools Extension 熟悉React的同学,可能对React Developer Tools并不陌生,     刚看到的时候,我也觉得很神奇, 因为React De ...

  6. Git-撤销(回退)已经add,commit或push的提交

    本文只阐述如何解决问题,不会对git的各种概念多做介绍,如果有兴趣可以点击下面的链接,进行详细的学习:Pro Git本文适用的环境 现在先假设几个环境,本文将会给出相应的解决方法:1. 本地代码(或文 ...

  7. 2017-2018-2 20165312实验二《Java面向对象程序设计》实验报告

    2017-2018-2 20165312实验二<Java面向对象程序设计>实验报告 实验中遇到的问题 1.增加MyUtil的测试类之后,TestCase是红色的,但是没有找到junit.j ...

  8. vim中行末去掉^M

    方式1: 输入 :%s/^M//g 方式2: 输入:%s/\r//g

  9. nginx的proxy_pass路径转发规则最后带/问题

    一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...

  10. JOSN转列格式(csv文件)

    推荐网站 https://json-csv.com/ 限制1M大小