编程 顺时针打印矩阵
来源:互联网 发布:手机淘宝优惠券转化 编辑:程序博客网 时间:2024/06/15 10:00
对于一个矩阵,请设计一个算法从左上角(mat[0][0])开始,顺时针打印矩阵元素。给定int矩阵mat,以及它的维数nxm,请返回一个数组,数组中的元素为矩阵元素的顺时针输出。
1.
class Printer {public: vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m) { vector<int> res; int floor, right, bottom; for(floor=0; floor<(n+1)/2&&floor<(m+1)/2; floor++){ right = m - 1 -floor; bottom = n - 1 - floor; for(int i=floor; i<=right; i++){ res.push_back(mat[floor][i]); } for(int j=floor+1; j<=bottom; j++){ res.push_back(mat[j][right]); } if(floor<bottom){ for(int i=right-1; i>=floor; i--){ res.push_back(mat[bottom][i]); } } if(floor<right){ for(int i=bottom-1; i>floor; i--){ res.push_back(mat[i][floor]); } } } return res; }};
class Printer {public: vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m) { vector<int>res; int up=0; int left=0; int down=n-1; int right=m-1; while(up<=down&&left<=right) { if(up==down) { for(int i=left;i<=right;i++) { res.push_back(mat[up][i]); } } else if(left==right) { for(int i=up;i<=down;i++) { res.push_back(mat[i][left]); } } else { int change=left; int temp=up; while(change!=right) { res.push_back(mat[up][change]); change++; } while(temp!=down) { res.push_back(mat[temp][right]); temp++; } while(change!=left) { res.push_back(mat[down][change]); change--; } while(temp!=up) { res.push_back(mat[temp][left]); temp--; } } left++; down--; up++; right--; } return res; }};
0 0
- 编程 顺时针打印矩阵
- 编程练习——顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 矩阵顺时针打印
- 顺时针打印矩阵
- 顺时针打印矩阵[算法]
- 顺时针打印矩阵
- 顺时针打印矩阵
- 顺时针打印矩阵
- 卡拉OK效果的实现-IOS音乐播放器
- 体验Impress.js
- 学习OpenCV2——控制视频暂停与播放
- leetcode 3Sum
- 方法与对象参数
- 编程 顺时针打印矩阵
- 并查集-poj2524-Ubiquitous Religions
- 复制构造函数为什么必须是引用?
- 编程技巧 - 封装技巧 - 2
- iOS面试总结
- C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
- 通过重写方法提高可用性,在此用作对多种不同要求的字段校验
- Template设计模式
- Android开发小知识-Spinner控件使用