zoukankan      html  css  js  c++  java
  • 【leetcode】Spiral Matrix

    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 class Solution {
     2 public:
     3     vector<int> spiralOrder(vector<vector<int> > &matrix) {
     4         int m=matrix.size();
     5         if(m==0) return vector<int>();
     6        
     7         int n=matrix[0].size();
     8        
     9         vector<int> result(m*n);
    10        
    11         vector<vector<bool> > visited(m,vector<bool>(n,false));
    12        
    13         vector<pair<int,int>> dir(4);
    14         dir[0]=pair<int,int>(0,1);
    15         dir[1]=pair<int,int>(1,0);
    16         dir[2]=pair<int,int>(0,-1);
    17         dir[3]=pair<int,int>(-1,0);
    18        
    19         int i,j,k,count;
    20         i=j=k=count=0;
    21  
    22         while(1)
    23         {              
    24             if(count==m*n) break;
    25  
    26             result[count]=matrix[i][j];
    27             visited[i][j]=true;
    28  
    29             if(i+dir[k].first>m-1||j+dir[k].second>n-1||i+dir[k].first<0||j+dir[k].second<0||visited[i+dir[k].first][j+dir[k].second])
    30             {
    31                 k=(++k)%4;
    32             }
    33  
    34             i+=dir[k].first;
    35             j+=dir[k].second;
    36            
    37             count++;
    38         }
    39        
    40         return result;
    41     }
    42 };
     
     
    另外一种方法:
     
     1 class Solution {
     2 public:
     3     vector<int> spiralOrder(vector<vector<int> > &matrix) {
     4         int m=matrix.size();
     5         if(m==0) return vector<int>();
     6        
     7         int n=matrix[0].size();
     8        
     9         vector<int> result;
    10        
    11        
    12         int x1=0;
    13         int y1=0;
    14         int x2=m-1;
    15         int y2=n-1;
    16        
    17        
    18         while(1)
    19         {
    20            
    21             for(int j=y1;j<=y2;j++) result.push_back(matrix[x1][j]);
    22             x1++;
    23            
    24            
    25             for(int i=x1;i<=x2;i++) result.push_back(matrix[i][y2]);
    26             y2--;
    27            
    28            
    29             if(x2+1!=x1)
    30                 for(int j=y2;j>=y1;j--) result.push_back(matrix[x2][j]);
    31             x2--;
    32            
    33             if(y1!=y2+1)
    34                 for(int i=x2;i>=x1;i--) result.push_back(matrix[i][y1]);
    35  
    36             y1++;
    37            
    38             if(result.size()==m*n) break;
    39         }
    40        
    41         return result;
    42     }
    43  
    44 };
  • 相关阅读:
    CentOS 7.3报Centos7_Base库缺少GPG公钥
    nginx重写(隐藏)index.php目录
    工作经历简写
    Centos7.4安装htop
    nginx 超时时间配置说明
    c#中数据从数据库到客户端主要几种的导出方式(导出到excel,导出到word)
    C#操作word文档如何设置表格的行高
    Windows计划任务定时启动程序运行时发生错误的解决办法
    Asp.Net MVC中请求json文件时返回404 not found的解决办法
    Angularjs 如何自定义Img的ng-load 事件
  • 原文地址:https://www.cnblogs.com/reachteam/p/4209678.html
Copyright © 2011-2022 走看看