LeetCode 54. Spiral Matrix
来源:互联网 发布:windows store被卸载了 编辑:程序博客网 时间:2024/06/02 05:36
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]
.
answer:
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> result; if(matrix.empty()) return result; int nLength = matrix[0].size(),mLength = matrix.size(); int n = nLength - 1; int m = 0;// cout << nLength << " " << mLength << endl; while(nLength > 0 && mLength > 0){ for(int i = 0; i < nLength; i ++) result.push_back(matrix[m][i + m]); for(int i = 1; i < mLength; i ++) result.push_back(matrix[i + m][n]); if(nLength == 1){ return result; } for(int i = nLength - 2; i >= 0 && mLength > 1; i --) result.push_back(matrix[m + mLength - 1][i + m]); for(int i = mLength - 2 ; i > 0; i --) result.push_back(matrix[i + m][m]); mLength -= 2; nLength -= 2; m ++; n --; } return result; } };
0 0
- 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
- Leetcode 54. Spiral Matrix
- [leetcode] 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- 54. Spiral Matrix LeetCode
- 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
- 开发博客期间看到的优秀资料
- (function(){})()
- 【Android 进阶】Retrofit2 目前最优雅的网络请求框架
- web测试专题技术
- 《经久不衰的Spring框架:SpringMVC 统括》
- LeetCode 54. Spiral Matrix
- 12、spring的bean基础(4)
- SPOJ : Interesting Subset 想法题
- jQuery的属性与样式
- 队列,栈和递归
- HDU1018_Big Number_取10的对数
- java NIO
- Java泛型
- 08--inline内联函数和宏替换