LeetCode: Spiral Matrix
来源:互联网 发布:阅读题软件 编辑:程序博客网 时间:2024/05/01 03:10
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) { if(matrix.size() == 0 || matrix[0].size() == 0) return result; print(0, matrix.size(), 0, matrix[0].size(), 0, matrix); return result; }private: vector<int> result; void print(int startX, int endX, int startY, int endY, int turn, vector<vector<int> > &matrix) { int x, y; if(startX >= endX || startY >= endY) return; turn = turn % 4; if(turn == 0) { y = startY; x = startX; while(y < endY) { result.push_back(matrix[x][y]); y++; } print(startX + 1, endX, startY, endY, turn + 1, matrix); } else if(turn == 1) { y = endY - 1; x = startX; while(x < endX) { result.push_back(matrix[x][y]); x++; } print(startX, endX, startY, endY - 1, turn + 1, matrix); } else if(turn == 2) { y = endY - 1; x = endX - 1; while(y >= startY) { result.push_back(matrix[x][y]); y--; } print(startX, endX - 1, startY, endY, turn + 1, matrix); } else { y = startY; x = endX - 1; while(x >= startX) { result.push_back(matrix[x][y]); x--; } print(startX, endX, startY + 1, endY, turn + 1, matrix); } }};
Round 2:
class Solution {public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> result; int m = matrix.size(); if(m == 0) return result; int n = matrix[0].size(); int x = 0, y = 0, carry = 0; while(result.size() < m*n) { while(x == carry && y < n-carry) { result.push_back(matrix[x][y]); y++; } y--; x++; if(x >= m-carry) break; while(y == n-1-carry && x < m-carry) { result.push_back(matrix[x][y]); x++; } x--; y--; if(y < carry) break; while(x == m-1-carry && y >= carry) { result.push_back(matrix[x][y]); y--; } y++; x--; if(x <= carry) break; while(y == carry && x > carry) { result.push_back(matrix[x][y]); x--; } x++; y++; carry++; } return result; }};
0 0
- [leetcode]Spiral Matrix&&Spiral Matrix II
- Leetcode:Spiral Matrix与Spiral Matrix II
- LeetCode | Spiral Matrix /Spiral Matrix II
- 【Leetcode】【python】Spiral Matrix/Spiral Matrix II
- LeetCode: Spiral Matrix
- LeetCode: Spiral Matrix II
- LeetCode Spiral Matrix
- LeetCode Spiral Matrix II
- [Leetcode] Spiral Matrix
- [Leetcode] Spiral Matrix II
- LeetCode: Spiral Matrix
- LeetCode : Spiral Matrix II
- [LeetCode] Spiral Matrix II
- [LeetCode] Spiral Matrix
- leetcode 80: Spiral Matrix
- [Leetcode] Spiral Matrix
- [LeetCode]Spiral Matrix
- [LeetCode]Spiral Matrix II
- caffe源码分析--Blob类代码研究
- 类模板声明与定义为什么不能分离
- git 命令纪要
- AWS Ubuntu下安装LAMP
- 稀疏矩阵A和B,其行数和列数对应相等,编写一个程序,计算A和B之和,假设稀疏矩阵采用三元组表示
- LeetCode: Spiral Matrix
- android项目两种构建方式的整合(Eclipse/idea和Android Studio)
- 解决了Cocoapods Undefined symbols for architecture _OBJC_CLASS_xxxx的问题,辛苦死我了,记录下之后有空在研究
- 在Oracle Linux 6.5上安装Oracle 10gR2 的最佳实践【Maclean版】
- Girl_iOS100天学iOS的第三天(Plist&UIPickerView)
- 输出好看的图形
- 安装mysql 5.5.8 Error Nr.1364 authentication_string
- 输入系统之led灯控制
- NodeJS会是昙花一现吗?