【LeetCode & 剑指offer刷题】矩阵题2:29 顺时针打印矩阵(54. Spiral Matrix)(系列)

【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

54. 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]
 
//以螺旋顺序输出矩阵元素(顺时针)
/*
确定某一层矩形对角点坐标,(begin,begin)、(rowend,colend)
扫描顺序如下:
-----→
↑    |
|    |
|    |
←----↓
*/
class Solution
{
public :
    vector < int > spiralOrder ( vector < vector < int >>& a )
    {
        vector < int > result ;
        if ( a . empty ()) return result ;
       
        int begin = 0 ; //起始点坐标
        int rowend = a . size () - 1 ; //对角点坐标
        int colend = a [ 0 ]. size () - 1 ;
       
        while ( begin <= rowend && begin <= colend )
        {
            for ( int j = begin ; j <= colend ; j ++) //输出上行 i = begin,j = begin ~ colend
            {
                result . push_back ( a [ begin ][ j ]);
            }
            for ( int i = begin + 1 ; i <= rowend ; i ++) //输出右边列 i = begin+1 ~ rowend, j = colend
            {
                result . push_back ( a [ i ][ colend ]);
            }
           
            if ( begin < rowend ) //防止最后只有一行
            {
                for ( int j = colend - 1 ; j >= begin ; j --) //输出下行 若begin<rowend, i = rowend, j = colend-1 ~ begin
                {
                    result . push_back ( a [ rowend ][ j ]);
                }
            }
           
            if ( begin < colend ) //防止最后只有一列
            {
                for ( int i = rowend - 1 ; i >= begin + 1 ; i --) //输出左边列 若begin<colend, i = rowend-1 ~ begin+1, j = begin
                {
                    result . push_back ( a [ i ][ begin ]);
                }                
            }
           
            begin ++; //缩至内圈
            rowend --;
            colend --;
        }
        return result ;
    }
};
 
59 .   Spiral Matrix II
Given a positive integer   n , generate a square matrix filled with elements from 1 to   n 2   in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
 
/*
问题:按螺旋的顺序来填数
Spiral Matrix 类似
*/
class Solution
{
public :
    vector < vector < int >> generateMatrix ( int n )
    {
        vector < vector < int >> a ( n , vector < int >( n ));
        if ( n < 1 ) return a ;
       
        int begin = 0 ; // 起始点坐标
        int rowend = n - 1 ; // 对角点坐标
        int colend = n - 1 ;
        int val = 1 ;
       
        while ( begin <= rowend && begin <= colend )
        {
            for ( int j = begin ; j <= colend ; j ++) // 赋值上行 i = begin,j = begin ~ colend
            {
                a [ begin ][ j ] = val ++; //++ 在后为先用后加,故 val 初值为 1
            }
            for ( int i = begin + 1 ; i <= rowend ; i ++) // 赋值右边列 i = begin+1 ~ rowend, j = colend
            {
                a [ i ][ colend ] = val ++;
            }
          
            if ( begin < rowend ) // 防止最后只有一行
            {
                for ( int j = colend - 1 ; j >= begin ; j --) // 赋值下行 begin<rowend, i = rowend, j = colend-1 ~ begin
                {
                    a [ rowend ][ j ] = val ++;
                }
            }
          
            if ( begin < colend ) // 防止最后只有一列
            {
                for ( int i = rowend - 1 ; i >= begin + 1 ; i --) // 赋值左边列 begin<colend, i = rowend-1 ~ begin+1, j = begin
                {
                    a [ i ][ begin ] = val ++;
                }               
            }
          
            begin ++; // 缩至内圈
            rowend --;
            colend --;
        }
        return a ;
    }
};
 

猜你喜欢

转载自www.cnblogs.com/wikiwen/p/10225008.html