Title:
Given a matrix of m x n elements (m rows, n columns), return all elements of the Matri X 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]
.
idea: Let's draw a spiral of our own walking trajectory. We will find that the helix always repeats four traversal processes, traversing to the right (column increments)--and traversing down (row increments) --> to the Left (column decrement) -->-up traversal (row decrement). But in the course of the spiral, the upper and lower bounds of our ranks are changing. So we maintain four variables, Rowbegin, Rowend, Colbegin, Colend. We use this four variable to indicate the bounds of the traversal. Traversal termination condition is Rowbegin > Rowend | | Colbegin > Colend. It is important to note that when we traverse to the left and walk up, we need the rows or columns to be traversed for existence and avoid duplication.
Attention:
1. Maintain four variables to define a helix traversal boundary.
int rowbegin = 0;int Rowend = row-1;int Colbegin = 0;int colend = col-1;
2. Spiral travel conditions must be met.
while (Rowbegin <= rowend && colbegin <= colend)
3. To prevent duplication, you need to first determine the row and column coordinate values when traversing to the left and walking up.
if (rowbegin <= rowend) //important judgment, prevent repeating { //left column descending traversal for (int j = colend; J >= Colbegin; j--) { Ret.push_back (Matrix[rowend][j]); } } rowend--; After traversal, remove this line if (colbegin <= colend) //important judgment, prevent repetition { //to the upstream decrement traversal for (int i = rowend; I >= Rowbegin; i--) { ret.push_back (Matrix[i][colbegin]); } } colbegin++; After traversal, remove this column
Complexity: O (n), n is the total number of elements of the matrix
AC Code:
Class Solution {public:vector<int> Spiralorder (vector<vector<int> > &matrix) {vector< int> ret; if (Matrix.empty ()) return ret; int row = Matrix.size (); int col = matrix[0].size (); int rowbegin = 0; int rowend = row-1; int colbegin = 0; int colend = COL-1; Spiral curve, motion trajectory always consistent while (Rowbegin <= rowend && colbegin <= colend) {//Right column increment traversal for (int j = colbegin, J <= Colend; j + +) {Ret.push_back (matrix[rowbegin][j]); } rowbegin++; After traversal, remove this line//to the downstream increment traversal for (int i = rowbegin; I <= rowend; i++) { Ret.push_back (Matrix[i][colend]); } colend--; After traversal, remove this column if (Rowbegin <= rowend)//important judgment to prevent repeat {//left column descending traversal for (int j = Colend; j>= Colbegin; j--) {ret.push_back (matrix[rowend][j]); }} rowend--; After traversal, remove this line if (Colbegin <= colend)//important judgment to prevent repetition {//descending on the upstream for (int i = rowend; I >= rowbegin; i--) {ret.push_back (Matrix[i][colbegin]); }} colbegin++; After traversal, remove this column} return ret; }};
[C + +] leetcode:110 Spiral matrix (Spiral output matrix Element)