Spiral Matrix
来源:互联网 发布:淘宝收藏店铺链接 填id 编辑:程序博客网 时间:2024/06/03 12:16
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]
You should return [1,2,3,6,9,8,7,4,5]
.
class Solution {public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> result; if (matrix.size() == 0) { return result; } int left = 0; int top = 0; int bottom = matrix.size() - 1; int right = matrix[0].size() - 1; int row = 0; int column = 0; while (left <= right && top <= bottom) { if (column <= right) { while (column <= right) { result.push_back(matrix[row][column]); column++; } column--; top++; row++; } if (left <= right && top <= bottom && row <= bottom) { while (row <= bottom) { result.push_back(matrix[row][column]); row++; } row--; right--; column--; } if (left <= right && top <= bottom && column >= left) { while (column >= left) { result.push_back(matrix[row][column]); column--; } column++; bottom--; row--; } if (left <= right && top <= bottom && row >= top) { while (row >= top) { result.push_back(matrix[row][column]); row--; } row++; left++; column++; } } return result; }};
0 0
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- Spiral Matrix
- java怎么实现系统监控、系统信息收集
- c/c++的&、异或、~、!等运算
- POJ2586 Y2K Accounting Bug (簡單貪心問題)
- 防止win7在任务栏预览窗关闭程序
- linux共享内存
- Spiral Matrix
- epoll精髓
- Ubuntu 14.04.2 LTS 配置 DNS server
- TinyMCE 去掉复制粘贴来的字符串中的标签
- ZOJ - 3594 Sexagenary Cycle(公元没有0年)
- 浅解Unicode与UTF-8
- CLion 1.0发布,C/C++跨平台集成开发环境
- 解说同源策略和跨域访问
- FEM之一维静态热传导