zoukankan      html  css  js  c++  java
  • 顺时针打印矩阵

     1 class Solution {
     2 public:
     3     vector<int> printMatrix(vector<vector<int> > matrix) {
     4         vector<int> result;
     5         if(matrix.empty()) return result;
     6         int rows = matrix.size();
     7         int cols = matrix[0].size();
     8         //定义四个角标
     9         int left = 0;
    10         int right = cols-1;
    11         int top_row = 0;
    12         int end_row = rows-1;
    13         while(left<=right && top_row<=end_row){
    14             //打印第一行
    15             for(int i=left;i<=right;i++) result.push_back(matrix[top_row][i]);
    16             //打印右边的列
    17             //判断边界,如果只有一行的情况就不push了
    18             if(top_row<end_row){
    19                 for(int i=top_row+1;i<=end_row;i++) result.push_back(matrix[i][right]);
    20             }
    21             //打印最下面一行
    22             //判断边界
    23             if(left<right && top_row<end_row){
    24                 for(int i=right-1;i>=left;i--) result.push_back(matrix[end_row][i]);
    25             }
    26             //打印左边一列
    27             //判断边界
    28             if(top_row+1<end_row && left<right){
    29                 for(int i=end_row-1;i>=top_row+1;i--) result.push_back(matrix[i][left]);
    30             }
    31             left++;right--;top_row++;end_row--;
    32         }
    33         return result;
    34     }
    35 };
  • 相关阅读:
    ecolise 设置反编译
    整理03
    JAVA创建对象的五种方式
    JAVA中的深复制和浅复制--建议多看几遍
    选择题
    python学习第九天
    python学习第八天
    python学习第七天
    python学习第五天
    python学习第四天
  • 原文地址:https://www.cnblogs.com/pacino12134/p/11143730.html
Copyright © 2011-2022 走看看