Spiral Matrix

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

For example,
Given the following matrix:

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

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

这道题用的递归的思想,将最外面一层添加到result列表中,然后将剩下元素作为一个数组,做下一次递归,感觉有点土,晚点去搜点高大上的

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. //class ListNode {
  6. // public int val;
  7. // public ListNode next;
  8. // ListNode(int x) {
  9. // val = x;
  10. // next = null;
  11. // }
  12. // }
  13.  
  14. public class Solution {
  15. List<Integer> result = new ArrayList<Integer>();
  16.  
  17. public List<Integer> spiralOrder(int[][] matrix) {
  18. if(null == matrix || matrix.length == 0)
  19. return result;
  20.  
  21. else if(matrix.length == 1 && matrix[0].length == 1) //只有一个元素直接添加
  22. result.add(matrix[0][0]);
  23. else if(matrix[0].length == 1){ //竖条
  24. for(int i = 0; i < matrix.length; i++){
  25. result.add(matrix[i][0]); //直接添加
  26. }
  27. }
  28. else if(matrix.length == 1){ //横条
  29. for(int i = 0; i < matrix[0].length; i++){
  30. result.add(matrix[0][i]);
  31. }
  32. }
  33. else {
  34. for(int i = 0; i < matrix[0].length; i++){ //添加第一排
  35. result.add(matrix[0][i]);
  36. }
  37. for(int i = 1; i < matrix.length; i++){ //添加最后一竖
  38. result.add(matrix[i][matrix[0].length - 1]);
  39. }
  40. for(int i = matrix[0].length - 2; i >= 0; i--){ //添加最后一排
  41. result.add(matrix[matrix.length - 1][i]);
  42. }
  43. for(int i = matrix.length - 2; i >= 1;i--){ //添加第一排
  44. result.add(matrix[i][0]);
  45. }
  46. if(matrix.length - 2 != 0 && matrix[0].length - 2 != 0){
  47. int next[][] = new int[matrix.length - 2][matrix[0].length - 2];
  48. for(int i = 1; i < matrix.length - 1; i++){
  49. for(int j = 1; j < matrix[0].length - 1;j++){
  50. next[i - 1][j - 1] = matrix[i][j];
  51. }
  52. }
  53. spiralOrder(next); //递归求解下一个矩阵的值
  54. }
  55.  
  56. }
  57.  
  58. return result;
  59. }
  60. }

Spiral Matrix的更多相关文章

  1. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  2. [LeetCode] Spiral Matrix 螺旋矩阵

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

  3. LeetCode - 54. Spiral Matrix

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

  4. 【leetcode】Spiral Matrix II

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

  5. 【leetcode】Spiral Matrix II (middle)

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

  6. 59. Spiral Matrix && Spiral Matrix II

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

  7. 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 ...

  8. Java for LeetCode 059 Spiral Matrix II

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

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  10. 1105. Spiral Matrix (25)

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

随机推荐

  1. 转: ios app架构设计

    http://keeganlee.me/post/architecture/20160107 看完这一系列文章后就知道怎么回答这类问题了: App架构设计经验谈:接口的设计 App架构设计经验谈:技术 ...

  2. .Net (MVC) 随机生成验证码

    以前一直对C#的GDI画图部分知识点不怎么用所以忘得差不多了,这两天正好公司要做一个博客系统,其中一个需求就是留言时为了防止恶意攻击必须填写验证码,正好借着这个机会复习了一下,以下是实现代码,写的比较 ...

  3. mvc的一些知识点

    MVC是微软2009对外公布的第一个开源的表示层框架,这是微软的第一个开源项目, MVC目的不是取代WebFrom开发,只是web开发的另外一种选择 1.MVC设计模式 M:Model 主要是存储或者 ...

  4. Android Studio 1.3新版体验

    Google发布的Android Studio最新版是 1.3 版,上周的I/O大会中三位Google工程师对Android Studio作了将近1小时的演讲: 之前一直习惯用Eclipse luna ...

  5. 运维人愿意听到的话 vs 不愿意听到的话

    在公司兼做了接近两年的远程运维工作,与内部打交道的过程中听到各种各样的话, 简单摘列一下那些似曾相识的愿意.不愿意听到的话: ”先别操作,帮忙先拷贝日志!我们调查一下答复!“  vs  ”你先重启一下 ...

  6. C#如何关闭一个窗口的同时打开另一个窗口

    在.net的WinForm程序中,如果是直接起动的Form作为主窗口,那么这个主窗口是不能关闭的,因为它维护了一个Windows消息循环,它一旦关闭了就等于声明整个应用程序结束,所以新打开的窗口也就被 ...

  7. Xcode中如何更改Bundle identifier

    1.如图所示,更改Info.plist 中的Bundle identifier

  8. ./configure:command not found 解决方法

    有些下载下来的源码没有MAKEFILE文件,但是会有MAKEFILE.IN 和 configure, MAKEFILE文件则由后两个文件生成. 如果执行: $./configure   提示错误:./ ...

  9. redis setnx 分布式锁

    private final String RedisLockKey = "RedLock"; private final long altTimeout = 1 * 60 * 60 ...

  10. 【转】理解依赖注入(IOC)和学习Unity

    IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection).作用:将各层的对象以松耦合的方式组织在一起,解耦,各 ...