54.Maximum Spiral Matrix
题目
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 | [ |
You should return [1,2,3,6,9,8,7,4,5]
.
大意
答案
1 | class Solution { |
思路
This is a very simple and easy to understand solution. I traverse right and increment rowBegin, then traverse down and decrement colEnd, then I traverse left and decrement rowEnd, and finally I traverse up and increment colBegin.
The only tricky part is that when I traverse down or left I have to check whether the row or col still exists to prevent duplicates.
tips
声明一个n个大小的vector<vector
vector<vector
Author: corn1ng
Link: https://corn1ng.github.io/2017/11/13/算法/leetcode54/
License: 知识共享署名-非商业性使用 4.0 国际许可协议