Spiral Matrix I

Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.

Example

Given n = 3,

You should return the following matrix:

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

分析:从上,右,下,左打印。
 1 public class Solution {
 2     /**
 3      * @param n an integer
 4      * @return a square matrix
 5      */
 6     public int[][] generateMatrix(int n) {
 7
 8         int[][] arr = new int[n][n];
 9         int a = 0;
10         int b = n - 1;
11         int k = 1;
12
13         while (a < b) {
14             for (int i = a; i <= b; i++) {
15                 arr[a][i] = k++;
16             }
17
18             for (int i = a + 1 ; i <= b - 1; i++) {
19                 arr[i][b] = k++;
20             }
21
22             for (int i = b ; i >= a; i--) {
23                 arr[b][i] = k++;
24             }
25
26             for (int i = b - 1 ; i >= a + 1; i--) {
27                 arr[i][a] = k++;
28             }
29
30             a++;
31             b--;
32         }
33
34         if (a == b) {
35             arr[a][b] = k;
36         }
37         return arr;
38     }
39 }

Spiral Matrix II

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

Example

Given the following matrix:

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

You should return [1,2,3,6,9,8,7,4,5].

分析:

拿到左上角和右下角的坐标,然后从上,右,下,左打印。然后更新坐标。

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

转载于:https://www.cnblogs.com/beiyeqingteng/p/5680666.html

Spiral Matrix I II相关推荐

  1. LeetCode:Spiral Matrix I II

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

  2. 59. Spiral Matrix II

    /** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...

  3. LeetCode 59. Spiral Matrix II

    59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  4. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  5. LeetCode Spiral Matrix II (生成螺旋矩阵)

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

  6. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  7. HDU OJ Matrix Swapping II

    Matrix Swapping II Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  8. C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...

  9. PAT1105:Spiral Matrix

    1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...

最新文章

  1. 小朋友也能看懂的机器学习
  2. D语言与C语言的互相调用
  3. contains与compareDocumentPosition方法详解
  4. 网络加速和优化控制常用管理
  5. 【风控流程】大数据风控代码逻辑
  6. CentOS 6.4 编译安装 gcc-4.8.0
  7. 知网首篇被引破万论文诞生!作者是曾两次“被迫转行”的他
  8. python 计算机基础
  9. ——Eigen介绍及简单使用(PCL库实现)
  10. 《软件设计精要与模式》之Factory Method模式
  11. MD5加盐(md5+salt)
  12. python PIL库的getdata()函数
  13. Retinex算法,图像色彩增强之python实现——MSR,MSRCR,MSRCP,autoMSRCR
  14. eclipse php 代码补全,phpeclipse代码提示
  15. 龙泉寺贤超法师:用 AI 为古籍经书识别、断句、翻译
  16. python编写木马攻击_Python编写简易木马程序
  17. C/C++基础 isfinite()函数
  18. DKN: Deep Knowledge-Aware Network for News Recommendation阅读笔记
  19. 二分法求中点最佳code写法
  20. 支付宝 微信后台不死的黑科技

热门文章

  1. vba基本操作 -- 常用功能
  2. 【读书笔记】MSDN 上关于加密解密的一个例子
  3. 在 Intellij IDEA 里使用 OpenJFX (JavaFX)
  4. [JSOI2008]最小生成树计数
  5. ScrollView常用(暂时用上了的)代理方法
  6. Telnet远程访问思科交换机、路由器
  7. .NET MVC异步调用中的Session问题
  8. 在Asp.net网页中使用接口
  9. mysql 1130 localhost_解决1130 Host 'localhost' is not allowed to connect to this MySQL server
  10. 如何根据对象获取到对应的表名_Excel VBA 常用对象二