Leetcode 54. Spiral Matrix (Medium) (cpp)
来源:互联网 发布:达内和尚观linux哪个好 编辑:程序博客网 时间:2024/06/01 08:53
Leetcode 54. Spiral Matrix (Medium) (cpp)
Tag: Array
Difficulty: Medium
/*54. Spiral Matrix (Medium)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> res;if (matrix.empty()) { return res; }int m = matrix.size() - 1, n = matrix[0].size() - 1, h = 0, l = 0;while (true) {for (int col = l; col <= n; col++) {res.push_back(matrix[h][col]); }if (++h > m) { break; }for (int row = h; row <= m; row++) {res.push_back(matrix[row][n]); }if (--n < l) { break; }for (int col = n; col >= l; col--) {res.push_back(matrix[m][col]); }if (--m < h) { break; }for (int row = m; row >= h; row--) {res.push_back(matrix[row][l]); }if (++l > n) { break; }}return res;}};
0 0
- Leetcode 54. Spiral Matrix (Medium) (cpp)
- Leetcode 59. Spiral Matrix II (Medium) (cpp)
- LeetCode 54.Spiral Matrix (Medium)
- Leetcode 54. Spiral Matrix (Medium) (java)
- [Leetcode 54, Medium] Spiral Matrix
- 【LeetCode】(54)Spiral Matrix(Medium)
- LeetCode 59.Spiral Matrix || (Medium)
- 【LeetCode】(59)Spiral Matrix II(Medium)
- LeetCode-59-Spiral Matrix II(Array)-Medium
- Leetcode 59. Spiral Matrix II (Medium) (java)
- leetcode 日经贴,Cpp code -Spiral Matrix II
- LeetCode 54. Spiral Matrix
- [LeetCode]54.Spiral Matrix
- LeetCode --- 54. Spiral Matrix
- [Leetcode] 54. Spiral Matrix
- [leetcode] 54.Spiral Matrix
- Leetcode-54.Spiral Matrix
- **LeetCode 54. Spiral Matrix
- 字符数字之间的转换
- 【POJ】2503 - Babelfish(STL)
- time wait
- tablayout
- Matlab 和 C++混合编程
- Leetcode 54. Spiral Matrix (Medium) (cpp)
- 用户级线程和内核级线程
- 【Window程序设计:第五版】翻译勘误 转载
- 算法竞赛入门经典(第二版)-刘汝佳-第四章 发放救济金
- Unknown character set: 'utf8mb4' bug解决
- Linux下配置tomcat和mysql
- 【iOS】Masonry使用案例讲解
- ios开发小知识点--显示label上加删除线
- 搭建struts2环境