zoukankan      html  css  js  c++  java
  • Leetcode-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, 3 ],
     [ 4, 5, 6 ],
     [ 7, 8, 9 ]
    ]
    

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

    Solution:

     1 public class Solution {
     2     public List<Integer> spiralOrder(int[][] matrix) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         int xLen = matrix.length;
     5         if (xLen==0) return res;
     6         int yLen = matrix[0].length;
     7         if (yLen==0) return res;
     8 
     9         int[] x = new int[]{0,1,0,-1};
    10         int[] y = new int[]{1,0,-1,0};
    11         boolean[][] printed = new boolean[xLen][yLen];
    12         for (int i=0;i<xLen;i++)
    13             Arrays.fill(printed[i],false);
    14         int direction = 0;
    15         int curX = 0, curY=0;        
    16         for (int i=0;i<xLen*yLen;i++){
    17             res.add(matrix[curX][curY]);
    18             printed[curX][curY] = true;
    19             int nextX = curX+x[direction];
    20             int nextY = curY+y[direction]; 
    21             //Determin the availability of next point.
    22             if (nextX>=xLen || nextX<0 || nextY>=yLen || nextY<0 || printed[nextX][nextY]){
    23                 direction = (direction+1)%4;
    24                 nextX = curX+x[direction];
    25                 nextY = curY+y[direction];
    26             }
    27             curX = nextX;
    28             curY = nextY;
    29         }
    30         return res;        
    31     }
    32 }

    Solution 2:

    We actually does not need the matrix to record the visited element, because each time the direction is changed, one row or one col becomes unavailable. We only need to record and update the start and end of the available rows and clos.

     1 public class Solution {
     2     public List<Integer> spiralOrder(int[][] matrix) {
     3         List<Integer> res = new ArrayList<Integer>();
     4         int xLen = matrix.length;
     5         if (xLen==0) return res;
     6         int yLen = matrix[0].length;
     7         if (yLen==0) return res;
     8 
     9         int[] x = new int[]{0,1,0,-1};
    10         int[] y = new int[]{1,0,-1,0};      
    11         int direction = 0;
    12         int curX = 0, curY=0;        
    13         int rowStart = 0, rowEnd = xLen-1, colStart=0, colEnd=yLen-1;
    14 
    15         for (int i=0;i<xLen*yLen;i++){
    16             res.add(matrix[curX][curY]);
    17             int nextX = curX+x[direction];
    18             int nextY = curY+y[direction]; 
    19             //Determin the availability of next point.
    20             if (nextX>rowEnd || nextX<rowStart || nextY>colEnd || nextY<colStart){
    21                 direction = (direction+1)%4;
    22                 nextX = curX+x[direction];
    23                 nextY = curY+y[direction];
    24                 //Update the availability of rows and cols according the direction change.
    25                 if (direction==1) rowStart++;
    26                 if (direction==2) colEnd--;
    27                 if (direction==3) rowEnd--;
    28                 if (direction==0) colStart++;
    29             }
    30             curX = nextX;
    31             curY = nextY;
    32         }
    33         return res;        
    34     }
    35 }
  • 相关阅读:
    C++高级程序员(廊坊+高薪)欢迎各种漂回家!(该职位已截止)
    utf8_unicode_ci和utf8_general_ci区别
    Percentencoding
    libiconv GNU Project Free Software Foundation (FSF)
    2013年1月6日北京交流会:当当网如何打造个性化推荐&精准营销生态系统
    COM Vs .NET (Qt ActiveQt)
    新一篇: Unicode字符编码规范 实例详细介绍各种字符集编码转换问题
    甩开外包,雄踞榜首:揭开“宫爆老奶奶”成功的秘密
    awk使用命令
    API SOCKET基础(三)网络字节序与主机字节序的转换
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4102804.html
Copyright © 2011-2022 走看看