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


题意:蛇形遍历二维数组

分类:数组


解法1:思路要顺畅,我们观察一个次遍历,可以发现,每次都是从(i,i)这个坐标开始,例如起始坐标是(0,0),一圈下来,坐标是(1,1)

于是循环结束条件是i*2<cols

对于每圈,首先计算出结束的x,y坐标,用于边界判断

最后从左到右,从上到下,从右到左,从下到上,完成一圈

其中要注意,不是每次都能向下走,这里我使用了一个二维数组来简单判断某个元素是否被访问过

从而判断是否能继续走,当然还有更加高效的办法,只是这个方法比较简单,不用考虑很多情况

[java]  view plain  copy
  1. public class Solution {  
  2.     public List<Integer> spiralOrder(int[][] matrix) {  
  3.         ArrayList<Integer> arr = new ArrayList<Integer>();    
  4.         int rows = matrix.length;    
  5.         if(rows==0return arr;  
  6.         int cols = matrix[0].length;    
  7.         boolean vis[][] = new boolean[rows][cols];    
  8.         int x = 0;                  
  9.         while(x*2<cols && x*2<rows){    
  10.             int endX = rows-x-1;    
  11.             int endY = cols-x-1;    
  12.             for(int i=x;i<=endY;i++){    
  13.                 if(!vis[x][i]){    
  14.                     arr.add(matrix[x][i]);    
  15.                     vis[x][i] = true;    
  16.                 }    
  17.             }    
  18.             for(int i=x+1;i<=endX;i++){    
  19.                 if(!vis[i][endY]){    
  20.                     arr.add(matrix[i][endY]);    
  21.                     vis[i][endY] = true;    
  22.                 }    
  23.             }    
  24.             for(int i=endY-1;i>=x;i--){    
  25.                 if(!vis[endX][i]){    
  26.                     arr.add(matrix[endX][i]);    
  27.                     vis[endX][i] = true;    
  28.                 }    
  29.             }    
  30.             for(int i=endX-1;i>x;i--){    
  31.                 if(!vis[i][x]){    
  32.                     arr.add(matrix[i][x]);    
  33.                     vis[i][x] = true;    
  34.                 }                   
  35.             }               
  36.             x = x + 1;              
  37.         }    
  38.         return arr;    
  39.     }  
  40. }  


解法2:优化每次过程,使用标志来记录每次的边界,包括left,top,right,bittom

[java]  view plain  copy
  1. public class Solution {  
  2.     public List<Integer> spiralOrder(int[][] matrix) {  
  3.         ArrayList<Integer> res = new ArrayList<Integer>();  
  4.         int n = matrix.length;  
  5.         if(n==0return res;  
  6.         int m = matrix[0].length;  
  7.         int x = 0,y = 0;  
  8.         int count = 0,limit = m*n;  
  9.         int right = m-1,bottom = n-1,left = 0,top = 0;  
  10.         while(count<limit){  
  11.             while(count<limit&&y<=right){res.add(matrix[x][y++]);count++;}  
  12.             y--;x++;top++;  
  13.             while (count<limit&&x<=bottom) {res.add(matrix[x++][y]);count++;}  
  14.             x--;y--;right--;  
  15.             while(count<limit&&y>=left) {res.add(matrix[x][y--]);count++;}  
  16.             x--;y++;bottom--;  
  17.             while(count<limit&&x>=top) {res.add(matrix[x--][y]);count++;}  
  18.             x++;y++;left++;  
  19.         }  
  20.         return res;  
  21.     }  
  22. }  



原文链接http://blog.csdn.net/crazy__chen/article/details/46401389

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值