54. Spiral Matrix
来源:互联网 发布:冯大辉 离职 知乎 编辑:程序博客网 时间:2024/05/19 11:38
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> ret; if(matrix.empty()) //matrix为空时返回空的vector<int> return ret; int a; int x_min = 0, y_min = 0; int x_max = matrix.at(0).size() - 1, y_max = matrix.size() - 1; while(true) { for(a = x_min; a <= x_max; a++) //把最上的一行的元素从左到右添加到ret ret.push_back(matrix.at(y_min).at(a)); y_min++; //相当于把最上的一行删除了 if(y_min > y_max) break; for(a = y_min; a <= y_max; a++) //把最右的一列的元素从上到下添加到ret ret.push_back(matrix.at(a).at(x_max)); x_max--; //相当于把最右的一列删除了 if(x_min > x_max) break; for(a = x_max; a >= x_min; a--) //把最下的一行的元素从右到左添加到ret ret.push_back(matrix.at(y_max).at(a)); y_max--; //相当于把最下的一行删除了 if(y_min > y_max) break; for(a = y_max; a >= y_min; a--) //把最左的一列的元素从下到上添加到ret ret.push_back(matrix.at(a).at(x_min)); x_min++; //相当于把最左的一列删除了 if(x_min > x_max) break; } return ret; }};
阅读全文
0 0
- 54. Spiral Matrix && 59. Spiral Matrix II
- 59. Spiral Matrix II && 54. Spiral Matrix
- LeetCode 54. Spiral Matrix
- [LeetCode]54.Spiral Matrix
- LeetCode --- 54. Spiral Matrix
- [Leetcode] 54. Spiral Matrix
- [leetcode] 54.Spiral Matrix
- 54. Spiral Matrix
- Leetcode-54.Spiral Matrix
- **LeetCode 54. Spiral Matrix
- Leetcode 54. Spiral Matrix
- [leetcode] 54. Spiral Matrix
- 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- 54. Spiral Matrix LeetCode
- 54. Spiral Matrix
- 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- Http以及TCP/IP协议
- python里使用正则表达式的非贪婪模式
- 《数据库SQL实战》不使用order by
- 动态规划-516. Longest Palindromic Subsequence
- 深度学习(4):优化神经网络(2)
- 54. Spiral Matrix
- 美妙仙人掌 cactus
- Android控件
- 大数四则运算模板
- 组装机主板boot menu
- c语言操作redis数据库
- 不错的Spring教程
- c++继承
- RxJava2.0学习笔记(Backpressure,Flowable)