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

     输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
    1
    class Solution { 2 public: 3 vector<int> printMatrix(vector<vector<int> > matrix) { 4 vector<int> res; 5 int row=matrix.size(); 6 if(row==0) return res; 7 int col=matrix[0].size(); 8 int i,j,left=0,right=col-1,up=0,down=row-1; 9 while(left<=right&&up<=down) 10 { 11 //上行 12 for(j=left;j<=right;j++) 13 res.push_back(matrix[up][j]); 14 //右列 15 for(i=up+1;i<=down;i++) 16 res.push_back(matrix[i][right]); 17 //下行 18 if(down>up) 19 for(j=right-1;j>=left;j--) 20 res.push_back(matrix[down][j]); 21 //左列 22 if(left<right) 23 for(i=down-1;i>up;i--) 24 res.push_back(matrix[i][left]); 25 up++; 26 down--; 27 left++; 28 right--; 29 } 30 return res; 31 } 32 };
  • 相关阅读:
    iOS中图片与视频一次性多选
    UIImagePickerController Class
    1月16日
    10月20日
    1月14日
    1月13日
    1月12日
    1月11日
    课程评价与建议
    加分总结
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/7760055.html
Copyright © 2011-2022 走看看