顺时针打印矩阵
来源:互联网 发布:英汉双语阅读软件 编辑:程序博客网 时间:2024/05/23 16:55
题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 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) { if(matrix.size()==0) return vector<int>{}; int n=matrix.size(); int m=matrix[0].size(); vector<int> res{}; int index=0; while(index<=n&&index<=m) { vector<int> temp= print(matrix,index,m-1,n-1); res.insert(res.end(),temp.begin(),temp.end()); ++index; --n; --m; } return res; } vector<int> print(vector<vector<int> > matrix,int index,int right,int bottom) { vector<int> res; if(index==right&&index<=bottom){ int temp=index; while(temp<=bottom){ res.push_back(matrix[temp++][index]); } } else if(index<right&&index==bottom) { int temp=index; while(temp<=right){ res.push_back(matrix[index][temp++]); } } else if(index<right&&index<bottom) { int i=index; int j=index; while(j<right) res.push_back(matrix[i][j++]); while(i<bottom) res.push_back(matrix[i++][j]); while(j>index) res.push_back(matrix[i][j--]); while(i>index) res.push_back(matrix[i--][j]); } return res; }};
0 0
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 矩阵顺时针打印
- 顺时针打印矩阵
- 顺时针打印矩阵[算法]
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- Subscription expires on 2017/2/25. Usage of PhpStorm will no longer be possible.
- 获取元素的样式
- 2014第五届蓝桥杯预赛试题本科c++史丰收速算
- 弹幕生成--辐射吸收体
- RecyclerView复用ViewPager出现的问题
- 顺时针打印矩阵
- 1002. A+B for Polynomials
- 让网络请求简单点——Volley框架(下)
- 多线程环境下处理未捕获的异常
- Problem Z-26
- codevs 1135 选择客栈 2011年NOIP全国联赛提高组
- 详解SQL 注入、XSS 攻击、CSRF 攻击
- 1009. Triple Inversions (35)
- 你必须『收藏』的Github技巧