鱼C论坛

 找回密码
 立即注册
查看: 1486|回复: 0

[学习笔记] leetcode 54. Spiral Matrix 谷歌面试题

[复制链接]
发表于 2019-8-18 03:29:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

  2. Example 1:

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

  11. Input:
  12. [
  13.   [1, 2, 3, 4],
  14.   [5, 6, 7, 8],
  15.   [9,10,11,12]
  16. ]
  17. Output: [1,2,3,4,8,12,11,10,9,5,6,7]
复制代码


思路:两个pointer,一个横着走,一个竖着走,每次计算matrix 的最外围一周,然后进行递归。考虑特殊情况,一个是empty set,另一个是size one array。

  1. class Solution {
  2.         public static List<Integer> spiralOrder(int[][] matrix) {

  3.         ArrayList<Integer> recept = new ArrayList<>();
  4.             
  5.         ArrayList<Integer> empty = new ArrayList<Integer>(){};
  6.             
  7.         if(matrix == null || matrix.length == 0){
  8.             
  9.             return empty;
  10.         }
  11.             
  12.         if(matrix.length == 1){
  13.             
  14.             for(int i = 0; i<matrix[0].length; i++ ){
  15.                
  16.                 recept.add(matrix[0][i]);
  17.             }
  18.             
  19.             return recept;
  20.         }

  21.         generateList(matrix, recept, matrix[0].length, matrix.length, 0);

  22.         return recept;
  23.     }

  24.     public static void generateList(int[][] matrix, ArrayList<Integer> array, int rowlen, int colen, int n) {
  25.         
  26.         if(matrix == null){

  27.             return;
  28.         }

  29.         int len_of_column = colen - 2*n;

  30.         int len_of_row = rowlen - 2*n;

  31.         if(len_of_column <= 0 || len_of_row <= 0) {

  32.             return;
  33.         }

  34.         int first = 0 + n, second = 0 + n;

  35.         if(first == n) {

  36.             while(first != n+ len_of_row && first>=n && first <= n+ len_of_row) {

  37.                 array.add(matrix[0+n][(first++)]);

  38. //                System.out.println(array);
  39. //                System.out.println("-----");
  40.             }
  41.         }

  42.         second++;

  43.         if(first == len_of_row+n && second == n+1) {

  44.             while(second != len_of_column + n && second >=n && second <= len_of_column + n) {

  45.                 array.add(matrix[second++][first-1]);

  46. //                System.out.println(array);
  47. //                System.out.println("-----");
  48.             }
  49.         }

  50.         first= first -2 ;

  51.         if(first == len_of_row-2+n && second == len_of_column+n) {

  52.             while(first != 0+n && first >=n && first <= len_of_row+n) {

  53.                 array.add(matrix[second-1][(first--)]);

  54. //                System.out.println(array);
  55. //                System.out.println("-----");
  56.             }
  57.         }

  58.         second--;



  59.         if(first == 0+n && second == len_of_column-1+n) {

  60.             while(second != n && second >=n && second <=len_of_column+n) {

  61.                 array.add(matrix[(second--)][first]);
  62.             }

  63.         }

  64.         n++;

  65.         generateList(matrix, array, rowlen, colen, n);

  66.     }
  67. }
复制代码


结果在running time 和 memory usage beat 100% online submissions!!!

17.jpg

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 21:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表