剑指offer--顺时针打印矩阵

题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:1 2 3 45 6 7 89 10 11 1213 14 15 16则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

分类:数组


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

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

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

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

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

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

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值