题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 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.

思想,用左上和右下的坐标定位出一次要旋转打印的数据,一次旋转打印结束后,往对角分别前进和后退一个单位。

class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
int row = matrix.size();
int col = matrix[].size();
vector<int> res;
// 输入的二维数组非法,返回空的数组
if (row == || col == ) return res;
//定义四个关键变量,表示左上和右下的打印范围
int left = , top = , right = col - , bottom = row - ;
while (left <= right && top <= bottom){
// left to right
for (int i = left; i <= right; ++i)
res.push_back(matrix[top][i]);
// top to bottom
for (int i = top + ; i <= bottom; ++i)
res.push_back(matrix[i][right]);
if (top != bottom)
for (int i = right - ; i >= left; --i)
res.push_back(matrix[bottom][i]);
if (left != right)
for (int i = bottom - ; i > top; --i)
res.push_back(matrix[i][left]);
left++,top++,right--,bottom--;
}
return res;
}
};

最新文章

  1. sqlmap和burpsuite绕过csrf token进行SQL注入检测
  2. 3dmax渲染插件,生成2.5d瓦片
  3. TX Textcontrol 使用总结三——禁用右键、模版合并
  4. Java线程池学习
  5. [转]每次打开IE9就会跳出管理加载项的窗口
  6. table固定宽度高度, 及overflow省略号
  7. hbase 导入导出、hbase shell 基本命令。
  8. 三方面搞定http协议之“报文模型”
  9. golang 常见疑惑总结
  10. java面试题总结
  11. Hadoop生态圈-Knox网关的应用案例
  12. SAS 数据集生成map 文件
  13. php 执行命令函数
  14. React Native控件之Picker
  15. 亚马逊如何变成 SOA(面向服务的架构)
  16. 【推导】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) B. Mystical Mosaic
  17. 【转载】RHEL / CentOS 7 用 Yum 安裝 MySQL 5.6
  18. 20145327高晨 实验一 &quot;Java开发环境的熟悉&quot;
  19. 2019 CCPC-Wannafly Winter Camp Day1 (Div2, onsite)
  20. matlab中滤波函数

热门文章

  1. Netty源码阅读之如何将TCP的读写操作和指定线程绑定
  2. Crawlab Lite 正式发布,更轻量的爬虫管理平台
  3. svg 使用中的疑惑点
  4. T2 监考老师 题解
  5. CUDA Programming Guide 学习笔记
  6. JAVA面向对象:三大特征 封装讲解
  7. 循序渐进nginx(一):介绍、安装、hello world、Location匹配
  8. Python源码剖析|百度网盘免费下载|Python新手入门|Python新手学习资料
  9. 前端学习(十六):JavaScript运算
  10. eclipse IDE usage of my own and tutorials link list