[LeetCode] 5.Array and String - Spiral Matrix spiral matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

The title comes from the idea fourth question diagonal traverse. We found that for two-dimensional array, it will be considered rectangle is a very good idea. In this method, four sets of four variables representing the four sides of the rectangular array considered. 
countUp, countDown, countLeft, countRight representing four sides up and down, taking int countLeft = 0, countRight = N -1, countUp = 0, countDown = M-1, where M and N are the number of rows and columns. Program from the first element of Matrix [0] [0] starting traverse security rules.
(Based on the experience of the fourth question, can quickly write code to this question, but more people suck the logic judgment condition setting If not careful, we will spend a lot of time on the logic bug adjustment)
Vector < int > spiralOrder (Vector <Vector < int >> & Matrix) {
     IF (matrix.size () == 0 ) return {}; 
    Vector < int > Result = {};
     int M matrix.size = (); // Get the number of two-dimensional array of rows 
    int N = Matrix [ 0 ] .size (); // get the number of columns of the two-dimensional array 
    int m = 0 , n-= 0 ; // counter, used to change the standard, and thus through the array 
    int countLeft = 0 , = N-countRight . 1 , COUNTUP = 0 , the countDown = M-1 ; // corresponding to the four sides of a rectangle, corresponding to the set value of the array index. 
    result.push_back (Matrix [m] [n-]); // first element added to the array output 
    the while (! result.size () = M * N) // until all array elements are added to the output of the loop end 
    {
         IF (n-m == == COUNTUP && countLeft) { // initial stage, from the array matrix [0] [0] starting traversal rectangular upper case. 
            the while (n-< countRight) { 
                n- ++ ; 
                result.push_back (Matrix [m] [n-]); 
            } 
            COUNTUP ++; // upper traverse completed, countUP moved downward 
        }
         the else  IF (m == COUNTUP - . 1N-== countRight &&) { // This is the case in the upper right corner of the rectangle, understanding m == countUp-1, as has been COUNTUP down. 
            the while (m < the countDown) { 
                m ++ ; 
                result.push_back (Matrix [m] [n-]); 
            } 
            countRight -; // the right traversal is completed, countRight leftward 
        }
         the else  IF (m = n-== && the countDown countRight + = . 1 ) // this is the case in the lower right corner of understanding. 1 + countRight == n- 
        {
             the while (n-> countLeft) { 
                n- - ; 
                result.push_back (Matrix [m] [n-]); 
            } 
            the countDown - ; 
        } 
        the else  IF (m + == the countDown . 1 && n-== countLeft) // This is the case in the lower left corner, the countDown understanding == +. 1 m 
        {
             the while (m> COUNTUP) { 
                m - ; 
                Result. push_back (Matrix [m] [n-]); 
            } 
            countLeft ++ ; 
        } 
        the else  IF (n-m == == COUNTUP && countLeft- . 1 ) { // this is the case in the upper left corner, understood n == countLeft-1 
            the while (n-< countRight) { 
                n- ++ ;
                result.push_back(matrix[m][n]);
            }
            countUp++;
        }
    }
    return result;
}

 



Guess you like

Origin www.cnblogs.com/hu-19941213/p/10935230.html